【问题标题】:Attaching pdf document from named Excel range into Outlook mail将命名 Excel 范围中的 pdf 文档附加到 Outlook 邮件中
【发布时间】:2020-09-06 11:59:26
【问题描述】:

我正在尝试将驱动器位置内的 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 

【问题讨论】:

  • .Attachments.Add 行中的代码中断” - 它是否给出任何具体的错误消息?
  • 错误代码状态验证路径和文件名是否正确

标签: excel vba pdf outlook attachment


【解决方案1】:

来自Microsoft documentation

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

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

响应 cmets:如果您的完整文件系统路径存储在名为“FilePathYTD”的命名范围中,那么您需要以与 .Subject 相同的方式对其进行处理:

.Attachments.Add Range("FilePathYTD").Value

【讨论】:

  • 是的,我理解,但是文件系统路径是动态的,我已将范围命名为“FilePathYTD”以引用我要附加的文件。因此,我希望这会起作用,但事实并非如此。如果我输入完整的文件系统路径,代码就可以工作,但这不会使其成为动态
  • @codingroadman53 在这种情况下,您需要Range("FilePathYTD").Value,就像您为.Subject 所做的那样 - 您当前传递给它的是String,而不是单元格引用。
  • 谢谢我现在已经做到了,但我不得不将命名单元格引用更改为包含 .pdf。想知道我是否可以直接在代码中完成此操作,因为当我输入 ("FilePathYTD.pdf").Value 时它不起作用
  • @codingroadman53 范围名称中不能有点或空格。您可以使用Range("FilePathYTD").Value & ".pdf" 连接文件扩展名
猜你喜欢
  • 2021-08-04
  • 2020-11-21
  • 2022-08-13
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2012-12-04
相关资源
最近更新 更多