How to Send Mass Personalized Emails with Excel VBA Programming
By: Dean Blank
Tags: Automation, Excel, Vba, Coding, Programming

How it Works

It’s best to get a high-level overview of the program before jumping into the code. The program works by iterating through a list of contacts (columns A to F in this example) and generating an email for each contact. The body of your email is formulated inside the input box (on the right). You can test if the email is in your desired format by clicking the first button, “Test Email Template,” which will display the email for the first contact only. Once you feel confident with the message format, click the second button, “Send Mass Email.”

The “Send Mass Email” button will generate personalized emails to everyone in your list of contacts. In the demonstration above, you will see that two emails are created when you click the “Send Mass Email” button because there are only two contacts in the list. Notice that the emails don’t automatically send, and instead display the email contents for you to check before clicking send. If you want to send the emails automatically without showing the composed email (like the demo above), you can uncomment the “send” property and comment out the “display” property in the VBA code. Don’t worry if you don’t understand the meaning of these properties yet; the code will be discussed soon in more detail.

Setup

Before you write your VBA program, you must set up your Excel sheet to let you run through a list contacts. In this example, I have the following columns for contact information: Name (A), Email (B), Subject (C), CC (D), Business (E), and Place Met (F). I also added two buttons to execute our VBA macros (Developer Tab -> Insert -> Button (Form Control)).

To add text to the body of the generated email, the user can interact with the text box (Insert Tab -> Text -> Text Box). Within the body, the user can put place holders to add customized text. For example, the opening statement says, “Hi C1,”. I used “C1” to represent column A’s values (you can use any place holder). In the code below, you will see how we replace “C1” with the recipients’ names from column A. Similarly, “C5” and “C6” from the text box are replaced with the values in columns E and F respectively.

Code

Now it’s time for the code! We will begin by creating the function to generate an email for the first contact only (Row 2). We will then modify this code to create emails for the entire list of contacts by adding a “Do While” loop.

In your text editor (Alt+F11), we start by declaring the variables we will use to send our emails (“Dim” statement). The variables include name, email, body, subject, copy (cc email), place, and business. Next, we can assign values to our variables by referencing the template we created in the setup. For example, to assign the email address in Cell B2 to our email variable, we can use the Range property: email = Range("B2").value .

Sub test_email_template() Dim name, email, body, subject, copy, place, business As String name = Split(Range("A2").Value, " ")(0) 'Grab first name only

email = Range("B2").Value

body = ActiveSheet.TextBoxes("TextBox 1").Text

subject = Range("C2").Value

copy = Range("D2").Value

business = Range("E2").Value

place = Range("F2").Value End Sub

As mentioned previously in the setup, we added place holders in the body of the email, such as “C1” for the name of the recipient. To change the value of the “C1” place holder to the name, you can use the “Replace” method to replace any instance of “C1” in the body text with the name variable: body = Replace(body,"C1",name) . Feel free to use the “Replace” method for all your place holders.

Sub test_email_template() Dim name, email, body, subject, copy, place, business As String name = Split(Range("A2").Value, " ")(0) 'Grab first name only

email = Range("B2").Value

body = ActiveSheet.TextBoxes("TextBox 1").Text

subject = Range("C2").Value

copy = Range("D2").Value

business = Range("E2").Value

place = Range("F2").Value ' replace place holders

body = Replace(body, "C1", name)

body = Replace(body, "C5", business)

body = Replace(body, "C6", place)



End Sub

Once our variables are created, we can now implement our email function. First, we declare our email objects, OutApp and OutMail, using the “Dim” statement. Then we use the “Set” method to create the Outlook objects, which contain the properties of the email (address to, cc, subject, body, attachments, display, send). With the OutMail object, you will be able to change the email properties below. After modifying your Outlook object, you will need to clear the objects by setting them to “Nothing.”

Sub test_email_template() Dim name, email, body, subject, copy, place, business As String

Dim OutApp, OutMail As Object name = Split(Range("A2").Value, " ")(0) 'extract first name

email = Range("B2").Value

body = ActiveSheet.TextBoxes("TextBox 1").Text

subject = Range("C2").Value

copy = Range("D2").Value

business = Range("E2").Value

place = Range("F2").Value





' replace place holders

body = Replace(body, "C1", name)

body = Replace(body, "C5", business)

body = Replace(body, "C6", place)





Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)

With OutMail

.to = email

.cc = copy

.subject = subject

.body = body

'.Attachments.Add ("") 'You can add files here

.display

'.Send

End With Set OutMail = Nothing

Set OutApp = Nothing MsgBox "Email(s) Sent!" End Sub

Ta-Da! The code above is now working and will generate an email for the first contact in your list. Notice in the Outmail object, the “display” property is uncommented, and “send” is commented; this means that the email will display and will not send. If you want to send the email on button-click without revealing its contents, comment out “display” and uncomment “send.”

Now it’s time to modify our program so it can generate emails for all contacts in our list. In the code below, we use a “Do While” loop to iterate through the items in the list. Within this loop, we will assign our variables to values based on the current row. For example, if we are on row 3 (i = 3), the name variable will be assigned the value in row 3 column 1. Once we have our variables assigned in the loop, we can use our OutMail object to generate an email for each contact using its corresponding variables.