将pdf文档从命名的excel范围附加到Outlook Mail VBA中

问题描述 投票:1回答:1

[基本,我敢肯定,但我想我已经接近将其付诸实践。

我正在尝试使用以下代码将驱动器位置内的pdf文件附加到邮件中。我认为这是我在命名范围上缺少的一些基本知识,如果有人可以帮助的话?

代码在.Attachments.Add行中中断。

谢谢

Sub Mail()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim mainfont As String
Dim headerfont As String
Dim subheaderfont As String
Dim closemain As String
Dim closeheader As String
Dim closesubheader As String

Dim Ash As Worksheet

Set Ash = ActiveSheet
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set OutMail = OutApp.CreateItem(0)

'construct email

With OutMail
.To = Range("To").Value
.cc = Range("Cc").Value
.Subject = Range("Subject").Value
.Attachments.Add ("FilePathYTD.pdf")
.Display

End With

End Sub 
excel vba pdf outlook attachment
1个回答
1
投票

Microsoft documentation

expression.Add (Source, Type, Position, DisplayName)来源|必填|变体|附件的来源。这可以是文件(由完整文件系统路径表示,并带有文件名)或构成附件的Outlook项目。((强化矿井)] >>

"FilePathYTD.pdf"不是完整的文件系统路径。 "C:\Users\codingroadman53\Documents\FilePathYTD.pdf"ThisWorkbook.Path & "\FilePathYTD.pdf"是。

作为对评论的回应:如果完整文件系统路径存储在名为“ FilePathYTD”的命名范围中,那么您需要使用与.Subject相同的方式来寻址它:

.Attachments.Add Range("FilePathYTD").Value
© www.soinside.com 2019 - 2024. All rights reserved.