Excel VBA中的AddAttachment无法在Outlook中附加文件

时间:2019-02-08 13:05:40

标签: excel vba outlook

我已经编写了这个非常简单的代码来在电子邮件中附加文件,但是电子邮件中没有附件。

它甚至不会引发任何错误。我已确保路径正确并且文件存在。 请帮助

Private Sub CommandButton2_Click()
On Error GoTo ErrHandler

    ' SET Outlook APPLICATION OBJECT.
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
    Dim Source_File As String

    ' CREATE EMAIL OBJECT.
    Dim objEmail As Object
    Set objEmail = objOutlook.CreateItem(olMailItem)

    With objEmail
        .To = "arushi.agarwal@in.ab-inbev.com"
        .Subject = "This is a test message k"
        .Body = "Please use this template for your weekly meeting today"
        .Send        ' SEND MESSAGE.
        .AddAttachment ("C:\Claims\Try.docx")
    End With

    ' CLEAR.
    Set objEmail = Nothing:    Set objOutlook = Nothing

ErrHandler:
    '

End Sub

1 个答案:

答案 0 :(得分:2)

我对您的代码进行了微小的更改,它对我有用。您还需要在发送电子邮件之前附加文件(例如,.send之前的.attachment)

Private Sub CommandButton2_Click()
    On Error GoTo ErrHandler

    ' SET Outlook APPLICATION OBJECT.
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
    Dim Source_File As String

    ' CREATE EMAIL OBJECT.
    Dim objEmail As Object
    Set objEmail = objOutlook.CreateItem(olMailItem)

    With objEmail
        .To = "arushi.agarwal@in.ab-inbev.com"
        .Subject = "This is a test message k"
        .Body = "Please use this template for your weekly meeting today"
        .Attachments.Add ("C:\Claims\Try.docx")
        .Send        ' SEND MESSAGE.
        '.AddAttachment ("C:\Claims\Try.docx")

    End With

    ' CLEAR.
    Set objEmail = Nothing:    Set objOutlook = Nothing
    Exit Sub
ErrHandler:
    Range("A1").Value = Err.Description
End Sub