【发布时间】:2020-11-21 10:33:07
【问题描述】:
我想在我的 Outlook 电子邮件中添加一个 .pdf 文件,该文件是通过 VBA Excel 发送的。
我的完整 Excel 代码看起来很像这样:
Sub Confirmationemail()
MsgBox ("The confirmation email will be sent now")
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim fs As Worksheet, bs As Worksheet
Dim Filename As String, Name As String, Name2 As String, Name3 As String, Reason As String
Dim Cost As String, PathFileName As String
Dim linecount2 As Long
ChDir ThisWorkbook.Path & "\"
Set fs = Sheets("Frontsheet")
Set bs = Sheets("BoM")
linecount2 = 1
Name = fs.Range("D10")
Name2 = fs.Range("D18")
Name3 = fs.Range("D38")
If fs.Range("D38").Value = 3 Then
Reason = fs.Range("K8")
ElseIf fs.Range("D38").Value = 4 Then
Reason = fs.Range("P4")
Else
Reason = fs.Range("K4")
End If
Filename = Name & "_" & Name2
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
With OutlookMail
.BodyFormat = olFormatHTML
.Display
.HTMLBody = "The job is ready. See the PDF version in the attachment"
.To = "xxx@xxx.co.uk; yyy@yyy-is.co.uk "
PathFileName = ThisWorkbook.Path & "\" & Filename & ".pdf"
.CC = "zzz@z-is.co.uk; www@wx-c.co.uk;"
.BCC = "yxks@ug.co.uk"
.Subject = Filename & "- Audit"
'.Attachments.Add PDFFile
myattachments.Add PathFileName
'.Attachments.Add Application.ActiveWorkbook.FullName
'.Send
End With
End Sub
我发现的最佳提示: How to attach exported pdf file to Outlook mail using Excel VBA? 但它指的是附加已经导出的PDF文档。合并一些代码片段不成功。
这里有一些解决方案: Attach PDF and send email via Outlook 但它只引用指定的单元格。
我发现的其他提示:
Excel VBA attaching print area as PDF.
Attach both pdf and excel files to an email on single click in VBA.
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_winother-mso_2010/attaching-a-pdf-file-in-vba-generated-email-in/527de6b4-66e6-4aa5-85b8-267a59ea6a7f
【问题讨论】:
-
将“强制”
Option Explicit放在顶部。更新帖子以指示错误所在的行和错误消息。