当我从 Excel 发送电子邮件时,为什么 Attachments.Add 不起作用?

Why doesn't Attachments.Add work when I send an Email from Excel?

我有一个工作簿,我从中创建了一个基于一系列单元格的 PDF。这一切都很好。我这样做与生成电子邮件是分开的,因此可以在通过电子邮件发送之前对其进行检查。 然后,我从同一个工作簿创建一封电子邮件,以随附 PDF 一起发送。电子邮件的正文是从工作簿的另一个单元格范围创建的。同样,这样做没有问题。 当我发送它时,问题就来了。电子邮件发送正常,电子邮件正文正常,但没有附件。

我已经三重检查了附件的文件路径(甚至将其移动到更简单的路径进行测试)并将其更改为附加一个简单的 word 文档。我还使用了两个不同的电子邮件提供商 1&1 和 GMail,但遇到了同样的问题。那个执着就是不想离开我

我还注意到,每当我将鼠标悬停在任何类型的 link 上时,鼠标指针都会显示一条消息。消息是:处理请求时出错 - 错误响应。我只能猜测它与我发出的所有测试电子邮件有关,但不知道它意味着什么或如何摆脱它。我还有事吗运行?

Sub CDO_Send_Email_Angebot()

    Dim Rng As Range
    Dim iMsg As Object
    Dim ws As Worksheet
    Dim Flds As Variant
    Dim iConf As Object
    Dim PdfFile As String

    PdfFile = Sheets("5_Angebot").Range("E97").Value & "." & Sheets("5_Angebot").Range("E98").Value

    'MsgBox rngAttachment

    '---------- Get the Emails from a cells on the sheet

    Dim SendItTo As String
    Dim SenderEmail As String
    Dim Subjectext As String

    SendItTo = Sheets("5_Angebot").Range("E94").Value
    SenderEmail = Sheets("5_Angebot").Range("E95").Value
    SubjectText = Sheets("5_Angebot").Range("E96").Value

    '---------

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1    ' CDO Source Defaults
    Set Flds = iConf.Fields
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SenderEmail

        '.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "**********"
        '.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.1and1.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "***********"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With
    ' ------
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Rng = Nothing
    On Error Resume Next

    Set Rng = Selection.SpecialCells(xlCellTypeVisible)
    Set Rng = Sheets("5_Angebot").Range("C101:J121")

    Set iMsg = CreateObject("CDO.Message")
    With iMsg
        Set .Configuration = iConf
        .To = SendItTo
        .From = SenderEmail
        .Subject = SubjectText

        .HTMLBody = RangetoHTML(Rng)

        '.Attachments.Add PdfFile
        .Attachments.Add ("D:\Corinne\test.docx")
        .Send
    End With
    Set iMsg = Nothing

    ' --------
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub

快速 google 搜索表明合适的方法是 AddAttachment,而不是 Attachments.Add(后者用于 MS Outlook)。您的方法调用中可能还有其他错误,所以我上面的建议仍然有效:debug without On Error Resume Next