【问题标题】:AddAttachment in excel VBA not able to attach file in outlookexcel VBA中的AddAttachment无法在Outlook中附加文件
【发布时间】:2019-02-11 13:08:00
【问题描述】:

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

它甚至不会抛出任何错误。我已确保路径正确且文件存在。 请帮忙

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

【问题讨论】:

  • 嗨 Arushi,您可以尝试使用 .Attachments.Add("C:\Claims\Try.docx") 而不是 .AddAttachment ("C:\Claims\Try.docx")
  • 您在添加附件之前发送电子邮件。
  • @Don't now Outlook,但也许你应该在 .AddAttachment.., 行下面写 .Send。
  • 天啊,我真傻。十分感谢大家。这真的很有帮助。

标签: excel vba outlook


【解决方案1】:

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

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

【讨论】:

  • 您可能希望在ErrHandler: 之前有一个Exit Sub,否则即使没有错误也会触发处理程序。
  • 非常感谢大家,这很有帮助:D
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 2022-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 2021-09-23
  • 1970-01-01
相关资源
最近更新 更多