VBA attaching PDF to email

cvdwatson

New Member
Joined
Oct 21, 2014
Messages
2
Hi all,
I have tried to learn just enough VBA to get a simple task done, and I'm stuck. Your guidance would be appreciated.
This basic code I copied and manipulated from Youtube...

I am setting this up in attempts to populate emails, each with a unique recipient, subject line, message, and attachment.
I have everything figured out except the attachment. Each unique attachment file path is in column F on sheet 1.

Also, in a perfect world, I would like the macro to loop the number of rows are in the workbook automatically, instead of the loop stopping at 6.

Code:

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.Display

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
Call SendEmail(Sheet1.Range("d" & row_number), Sheet1.Range("a" & row_number), Sheet1.Range("e" & row_number))
Loop Until row_number = 6

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the boards!
Make sure you enclose your code in code brackets.

I think the following simple addition will get you where you need to be.

Rich (BB code):
Sub SendEmail(what_address As String, subject_line As String, mail_body As String, mail_attachment As String)

Dim olapp As Outlook.Application
Set olapp = CreateObject("outlook.application")
Dim olmail As Outlook.MailItem
Set olmail = olapp.CreateItem(olMailItem)

olmail.Display

olmail.To = what_address
olmail.Subject = subject_line
olmail.Body = mail_body
olmail.Attachments.Add mail_attachment
'olmail.send

End Sub
Sub SendMassEmail()


For row_number = 2 to Sheet1.Range("A"&Rows.Count).End(xlUp).Row
DoEvents
Call SendEmail(Sheet1.Range("d" & row_number), Sheet1.Range("a" & row_number), Sheet1.Range("e" & row_number), Sheet1.Range("f" & row_number))
Next row_number

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,106
Messages
6,128,863
Members
449,473
Latest member
soumyahalder4

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top