【发布时间】: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 上看到的文件名。我不习惯编码。如果我的语言给你带来了麻烦,请见谅。我会试试你的代码