简体   繁体   English

在 Outlook 中附加 Pdf 文件附件的运行时错误

[英]Runtime error attaching Pdf file attachment in Outlook

This code for automated email with Pdf attachment is working in several laptops but not working in my laptop.带有 Pdf 附件的自动 email 代码在几台笔记本电脑上工作,但在我的笔记本电脑上不工作。 I am using same version of Excel and Windows.我正在使用相同版本的 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

It is a matter of Attachment full path necessity ...这是附件完整路径必要性的问题...

If strTempFile = Sheets("Sheet1").Range("B50").Value does not contain the path, Selection.ExportAsFixedFormat will work and it will create the file in the current directory .如果strTempFile = Sheets("Sheet1").Range("B50").Value不包含路径,则Selection.ExportAsFixedFormat将起作用,并将在当前目录中创建文件。

objMail.Attachments.Add strTempFile needs the full qualified file path . objMail.Attachments.Add strTempFile需要完整的限定文件路径

So, strTempFile must be fully qualified.因此, strTempFile必须是完全限定的。 I mean, try the next approach:我的意思是,尝试下一种方法:

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

If it will work, you can find any other temporary path (Temp, Documents etc.)如果可行,您可以找到任何其他临时路径(临时、文档等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM