Attach PDF and send email via Outlook

天大地大妈咪最大 提交于 2021-02-10 14:26:18

问题


I wrote a VBA code to send an email from a distribution list in Excel.

I want to attach a PDF. (It is one PDF on my hard drive.)

Column A is email address, column C is name, column E has the path to the PDF & cell J2 has what the body of the email will say.

How do I attach the pdf with the path in column E?

I have only 4 rows that I'm testing, however I'd like to send everything until the last row, which could vary. It's actually two macros, it would be nice if it were only one.

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

回答1:


Assuming that everything else in the code is working as expected. Then, you would need to add the following line before the olMail.Send method. Example:

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

More information on the attachments.add method can be found here Attachments Add

The send mail function would look like this :

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

Edit 1: I guess your question was how to include multiple attachments into an email. Then you can try this :

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


来源:https://stackoverflow.com/questions/55345015/attach-pdf-and-send-email-via-outlook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!