【问题标题】:How to add an attachment to an email using VBA in Excel如何在 Excel 中使用 VBA 将附件添加到电子邮件
【发布时间】:2018-07-24 08:34:44
【问题描述】:

我有以下代码,但它不工作。我对 VBA 也很陌生。该代码可用于填充电子邮件模板,但只要我添加 .Attachment.Add 它就不起作用。

Sub CreateMail()

Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With ActiveSheet
    Set rngTo = .Range("E2")
    Set rngSubject = .Range("E3")
    Set rngBody = .Range("E4")
    .Attachments.Add "Z:\PHS 340B\Letters of Non-Compliance\..Resources\W9 Form\VPNA W-9 01 09 2017"
End With

With objMail
    .to = rngTo.Value
    .Subject = rngSubject.Value
    .Body = rngBody.Value
    .Display 'Instead of .Display, you can use .Send to send the email _
                or .Save to save a copy in the drafts folder
End With

Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing

End Sub

【问题讨论】:

  • 你确定路径正确吗?
  • 是的,我直接从共享网络驱动器复制了它
  • 错误提示“对象不支持此属性或方法”

标签: excel vba outlook attachment


【解决方案1】:

试试这个:

Sub emailtest()

Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With ActiveSheet
Set rngTo = .Range("E2")
Set rngSubject = .Range("E3")
Set rngBody = .Range("E4")
End With

With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.Body = rngBody.Value
.Attachments.Add "Z:\PHS 340B\Letters of Non-Compliance\..Resources\W9 Form\VPNA W-9 01 09 2017"
.Display 'Instead of .Display, you can use .Send to send the email _
            or .Save to save a copy in the drafts folder
End With

Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing

End Sub

在 Outlook 而不是 Excel 中工作时,您需要使用 .Attachments.Add。

【讨论】:

  • 这是什么意思?我正在使用 Outlook 创建电子邮件,它只有在我尝试添加附件时才能正常工作。
  • 附件是.pdf
  • @Twinkievizzio11 尝试复制并粘贴我发布的代码,它在我测试时有效。你有 Attachments.Add 在你的“With Activesheet”中,你需要它在“With objMail”下。 .
  • 非常感谢!!完美运行。你是最棒的!
  • @Twinkievizzio11,请务必将 Ziggus 的回答标记为已接受。只需单击他帖子上向下投票箭头下方的复选标记即可。另外,我想补充一点,这是因为您错误地将附件添加到错误的代码区域下。对象ActiveSheet 不支持.Attachments.Add,因此需要将其移至With objMail 区域。对于仍然感到困惑的任何人,请稍作澄清。 ?
【解决方案2】:

这个简单的脚本应该说明如何将附件添加到电子邮件中,然后发送电子邮件。

Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2016
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .to = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hi there"
        .Attachments.Add ActiveWorkbook.FullName
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Send   'or use .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

https://www.rondebruin.nl/win/s1/outlook/amail1.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多