【问题标题】:Runtime error attaching Pdf file attachment in Outlook在 Outlook 中附加 Pdf 文件附件的运行时错误
【发布时间】:2020-12-04 02:57:14
【问题描述】:

此带有 Pdf 附件的自动电子邮件代码在几台笔记本电脑上工作,但在我的笔记本电脑上不工作。我使用的是相同版本的 Excel 和 Windows。

Sub SendWorksheet_AsPDFAttachment_OutlookEmail()
    Dim objFileSystem As Object
    Dim strTempFile As String
    Dim objOutlookApp As Outlook.Application
    Dim objMail As Outlook.MailItem
 
    'Specify the worksheet name
    Sheets("Sheet1").Activate
    
    Sheets("Sheet1").Range("A1:O36").Select
 
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTempFile = Sheets("Sheet1").Range("B50").Value
 
    'export the specific worksheet as PDF
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTempFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    'Call SaveAsPDF_Click
    'Create a new email
    
    Dim rng As Range
    'Dim imagerange As Range
    
    Set rng = Worksheets("Email Body").Range("A3:I23").SpecialCells(xlCellTypeVisible)
    'Set imagerange = Worksheets("Email Body").Range("A26:I45").SpecialCells(xlCellTypeVisible)
    Set objOutlookApp = CreateObject("Outlook.Application")
    Set objMail = objOutlookApp.CreateItem(olMailItem)
    objMail.Subject = "Letter of Appreciation from Office of CGPD"
 objMail.To = Sheets("Sheet1").Range("B49").Value
    'Attach the PDF file
    objMail.Attachments.Add strTempFile
   
    objMail.HTMLBody = RangetoHTML(rng)
    'objMail.Display '==>display this email
    objMail.send '==>to send this email
    'Delete the temp PDF file
    objFileSystem.DeleteFile (strTempFile)
End Sub

【问题讨论】:

  • 只是添加错误发生在调试下面的代码objMail.Attachments.Add strTempFile
  • 使用strTempFile = Sheets("Sheet1").Range("B50").Value时,讨论的单元格是否包含'pdf'扩展名?我会尝试搜索这个特定的文件,它应该保存在哪里,看看它的真实名称是怎样的......它是否正是 strTempFile 变量保存的那个?
  • =CONCATENATE("Letter - ",'Details Sheet'!B2,".pdf") 是 B50 上的公式,当看到 strTempFile - 文件名显示为 Letter -Raja.Pdf
  • 你看到这样一个新创建的文件了吗?如果是,您在哪里寻找?保存在当前目录下,但附件恐怕需要全路径。我建议您完全限定路径。我会尝试strTempFile = thisWorbook.Path & "\" & Sheets("Sheet1").Range("B50").Value。请用这种方法试试。
  • 我在调试模式下通过将鼠标指针放在 strTempfile 上看到的文件名。我不习惯编码。如果我的语言给你带来了麻烦,请见谅。我会试试你的代码

标签: vba pdf outlook


【解决方案1】:

这是附件完整路径必要性的问题...

如果strTempFile = Sheets("Sheet1").Range("B50").Value 不包含路径,Selection.ExportAsFixedFormat 将起作用,它会在当前目录中创建文件。

objMail.Attachments.Add strTempFile 需要完整的限定文件路径

所以,strTempFile 必须是完全合格的。我的意思是,尝试下一种方法:

strTempFile = thisWorbook.Path & "\" & Sheets("Sheet1").Range("B50").Value

如果可行,您可以找到任何其他临时路径(临时、文档等)

【讨论】:

    猜你喜欢
    • 2020-11-21
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多