【问题标题】:Attach PDF and send email via Outlook附加 PDF 并通过 Outlook 发送电子邮件
【发布时间】:2019-03-25 19:19:59
【问题描述】:

我编写了一个 VBA 代码,用于从 Excel 中的通讯组列表发送电子邮件。

我想附上一个 PDF。 (这是我硬盘上的一个 PDF。)

A 列是电子邮件地址,C 列是名称,E 列是 PDF 的路径,而单元格 J2 是电子邮件正文的内容。

如何在 E 列中附加带有路径的 pdf?

我只有 4 行要测试,但是我想发送所有内容,直到最后一行,这可能会有所不同。实际上是两个宏,如果只有一个就好了。

Sub SendEmail(what_address As String, subject_line As String, mail_body As String)

    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")

    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)

    olMail.To = what_address
    olMail.Subject = subject_line
    olMail.Body = mail_body
    olMail.Send

End Sub


Sub SendMassEmail()

    row_number = 1

    Do
        DoEvents
        row_number = row_number + 1
        Dim mail_body_message As String
        Dim full_name As String
        mail_body_message = Sheet2.Range("J2")
        full_name = Sheet2.Range("c" & row_number)
        mail_body_message = Replace(mail_body_message, "replace_name_here", full_name)
        Call SendEmail(Sheet2.Range("A" & row_number), "Request for Tax Exemption Certificate", mail_body_message)
    Loop Until row_number = 4

End Sub

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    假设代码中的其他所有内容都按预期工作。然后,您需要在 olMail.Send 方法之前添加以下行。示例:

    olMail.attachments.Add Sheets("Sheet1").Range("E2").Value, olByValue, , "sampleFile"
    

    关于 attachments.add 方法的更多信息可以在这里找到Attachments Add

    发送邮件功能如下所示:

    Sub SendEmail(what_address As String, subject_line As String, mail_body As String)
    
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
    
    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)
    
    olMail.To = what_address
    olMail.Subject = subject_line
    olMail.Body = mail_body
    olMail.attachments.Add Sheets("Sheet1").Range("E2").Value, olByValue, , "sampleFile"
    olMail.Send
    
    End Sub
    

    编辑 1:我猜您的问题是如何在电子邮件中包含多个附件。然后你可以试试这个:

    Sub SendEmail(what_address As String, subject_line As String, mail_body As String)
    
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
    Dim olMail As Outlook.MailItem
    Dim lastRow As Integer
    
    Set olMail = olApp.CreateItem(olMailItem)
    
    'Provide the Column where the attachment links are stored. I guess its E in your case
    lastRow = Sheets("Sheet1").Range("E" & Rows.Count).End(xlUp).Row
    
    olMail.To = what_address
    olMail.Subject = subject_line
    olMail.Body = mail_body
    
    'Loop through the column and add the attachments to the Email
    For i = 2 To lastRow
        .attachments.Add Sheets("Sheet1").Range("E" & i).Value, olByValue
    Next i
    olMail.Send
    
    End Sub
    

    【讨论】:

    • 效果很好。太感谢了!我需要添加什么让它循环到最后,而不仅仅是那 3 行?
    • ‘i=2’ 循环从第二行开始挑选数据。假设第一行充满了标题。
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2011-09-14
    • 2012-03-03
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    相关资源
    最近更新 更多