• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Use VBA to create a Mail Merge from Excel

Slipstick Systems

› Developer › Use VBA to create a Mail Merge from Excel

Last reviewed on October 2, 2018     30 Comments

Am Outlook user had a question:

I'm trying to send a mail merge from multiple people. I was wondering if there was a way to choose the sender address based on the recipient email address? I'm using an Excel file as the source of the merge and the sender names associated with the recipients in the excel file so I can add a field after the closing line in the document to have the senders name appear.

No, Outlook can’t do this, at least not as a native feature within the mail merge. Mail merges will always be sent from the default email account. However, it is possible if you use a macro to handle the merge, not the mail merge wizard.

This macro reads values from an Excel worksheet and sends a mail merge, replacing unique values in the Outlook template with values in the worksheet. It also sends the message From an address in the worksheet.
use values in a worksheet

Create an Outlook template, entering unique values where the merge fields would be entered. While you could use standard merge fields or bookmarks, you would need to use the Word Object Library to update the fields. Using unique values allows us to use VBA's Replace function.
template containing the fields

The finished merge will look like the following. (Yes, I know, I'm not creative with demo values and prefer to use Excel's features to create demo values.)
merged message

' This is an Excel macro
' you need to set a reference to Outlook Object Library
Public Sub SendMailMergeExcel()

 Dim olApp As Object
 Dim xlApp As Excel.Application
 Dim xlWB As Excel.Workbook
 Dim xlSheet As Excel.Worksheet
 Dim rCount As Long
 Dim bXStarted As Boolean
 Dim enviro As String
 Dim appdata As String
 Dim strPath As String
 Dim strAttachPath As String
 Dim SendTo As String
 Dim CCTo As String
 Dim  strSubject As String
 Dim  strAcctMgrName As String
 Dim  AcctMgrEmail
 
 Dim olItem As Outlook.MailItem
 Dim Recip As Outlook.Recipient
 
' Get Excel set up
enviro = CStr(Environ("USERPROFILE"))
appdata = CStr(Environ("appdata"))
     On Error Resume Next
     Set xlApp = Excel.Application
     On Error GoTo 0
     'Open the workbook to input the data
     Set xlWB = xlApp.ActiveWorkbook
     Set xlSheet = xlWB.Sheets("Sheet1")
    ' Process the message record
    
    On Error Resume Next

rCount = 2
strAttachPath = enviro & "\Documents\Send\"
Set olApp = GetObject(, "Outlook.Application")
     If Err <> 0 Then
         Set olApp = CreateObject("Outlook.Application")
         bXStarted = True
     End If

Do Until Trim(xlSheet.Range("A" & rCount)) = ""

strFirstname = xlSheet.Range("A" & rCount)
SendTo = xlSheet.Range("B" & rCount)
CCTo = xlSheet.Range("C" & rCount)
strSubject = xlSheet.Range("D" & rCount)
' if adding attachment
'strAttachment = strAttachPath & xlSheet.Range("E" & rCount)
strAcctMgrName = xlSheet.Range("F" & rCount)
AcctMgrEmail = xlSheet.Range("G" & rCount)

'Create Mail Item and view before sending
' Default message form
'Set olItem = olApp.CreateItem(olMailItem)

' use a Template
Set olItem = olApp.CreateItemFromTemplate(appdata & "\Microsoft\Templates\macro-test.oft")

With olItem
.SentOnBehalfOfName = AcctMgrEmail
.To = SendTo
.CC = CCTo
.Subject = strSubject

.Body = Replace(.Body, "[FirstName]", strFirstname)
.Body = Replace(.Body, "[AcctMgrEmail]", AcctMgrEmail)
.Body = Replace(.Body, "[strAcctMgrName]", strAcctMgrName)
if adding attachments: 
'.Attachments.Add strAttachment
.Save

.Display
'.Send
End With

  rCount = rCount + 1
  
  Loop

Set xlWB = Nothing
Set xlApp = Nothing
     
End Sub

How to use the macros on this page

First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.

To check your macro security in any Office 2010 application and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In older versions look at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

Set a reference to other Object Libraries
If you receive a "User-defined type not defined" error, you need to set a reference to another object library.

  1. Go to Tools, References menu.
  2. Locate the object library in the list and add a check mark to it.
    Reference the Word object model in Outlook's VBA Editor

More information as well as screenshots are at How to use the VBA Editor

Use VBA to create a Mail Merge from Excel was last modified: October 2nd, 2018 by Diane Poremsky
Post Views: 91

Related Posts:

  • Send Email to Addresses in an Excel Workbook
  • Log Messages and Attachment Names
  • Macro to Export Outlook Fields to Excel
  • Use VBA to Export Exchange GAL to Excel

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Comments

  1. Lanny Newville says

    April 10, 2021 at 8:26 pm

    Got this working pretty quickly, thanks for the well documented example. The template I created had some formatting including use of bold, underline and an embedded logo. They were stripped from the emails that were generated by the script. Is there a way to allow the formatting and image in the final product?

    Reply
    • Diane Poremsky says

      April 10, 2021 at 9:37 pm

      Try changing .Body to .HTMLBody.

      Reply
  2. Marin says

    March 16, 2021 at 8:17 am

    Thank you, it is working just fine.
    Small note, to avoid that warning window regarding the use of macro, in Trust Center Settings/Programmatic Access I had to check the option button "Never warn me about ..."
    Thank you again

    Reply
  3. Areti says

    November 19, 2020 at 11:41 am

    Oh wow - I think you have saved me hours and hours of work. Took me a bit of time to work out the adjustments I should make to the code for field names, file location etc but this is so, so helpful. Thanks for sharing your expertise.

    Reply
  4. Carl says

    April 27, 2020 at 10:39 am

    hello there, I’m using the code in word mail merge to change the subject already. I was wondering if that’s possible to only copy multiple people for different people as well, yours looks much complicated as I’m just sending out the same email message to & cc different people. (e.g.: A1 as the recipient, cc people in cell A2,A3,A4, second one B1 as the recipient, cc people in cell B2,B3,B4).

    I would love to hearing from you.

    Reply
  5. Jasim says

    March 7, 2020 at 12:20 pm

    Hi
    i am looking for some simple vba code to send bulk emails used a word template and data from an excel work book worksheet
    the mail body drafted on the templates with dynamic merge fields
    the mail subject should pull form the excel work book data sheet

    it is an advanced option if can be added for selecting batch emailing like 50's or 100's also at the end of the mailing a message box pop up says "number of mails sent"

    the mail sending button should be assigned on excel workbook and the template can keep in the same source folder of excel.

    its is much appreciated if you can help me in this

    Reply
  6. Akhilesh Sharma says

    March 4, 2020 at 4:10 pm

    Many Thanks for this useful code and info.
    One thing i am not able to do is attachments to the mail merge. Please support.

    Reply
  7. Georgios Pyrgiotakis says

    September 11, 2019 at 12:53 pm

    Hi Diane,

    this is almost working for me. It composes the emails, but the body is empty. I have created a template and pointed to the right path, but I get blank emails.

    Reply
    • Georgios Pyrgiotakis says

      September 12, 2019 at 1:43 pm

      Found the issue. A small typo at the path.

      Reply
  8. Bobby Yeung says

    June 20, 2019 at 6:53 am

    Hello, I'm really loving your explanations!

    Hopefully an easy one for you - I'm trying to add bespoke attachments for each recipient. For example, the file path for the first attachment is "C:\Users\MEtheUSER\Documents\Charlotte test.pdf". I've put this into column E as per your example above. However, nothing ever comes up! I would be so grateful if you could help solve this as I've spent the past 2 hours trying different iterations (including looking at the "strAttachPath" bit of your code).

    Thank you so much.

    Reply
  9. Christian says

    June 6, 2019 at 4:53 pm

    Hello,
    This is working good so far. However, when I tried to send a test message to myself, I'm still getting the [FirstName] field. I even changed the template to {FirstName} to see if that helps; it showed the [FirstName] field. I'm not sure if I did something wrong here.
    Can I please have some assistance with this?

    Reply
    • Diane Poremsky says

      June 7, 2019 at 1:07 am

      Copy the text inside the quotes in the macro and paste it in the template (or copy from the template and paste in the code) - the code looks for the exact match and replaces it. It needs to be identical, so to the upper and lower case characters.

      Reply
      • Christian says

        June 7, 2019 at 9:20 am

        I just tried what you had suggested and it didn't work.
        I think the problem I was having came from the variable where the first name is supposed to go. It picked up everything else just fine. It's like it completely skipped the first column. I had to declare the "strFirstname" variable to see if that will make a difference.
        I also changed the template name on both sides.

      • Christian says

        June 7, 2019 at 11:07 am

        (My previous question got deleted somehow, so let me try this again...)
        I tried what you had suggested, but it did not work.
        I was able to get my other columns just fine, but just not my 1st column oddly enough. I had to declare the "strFirstname" variable to see if that helps any; it wasn't declared before. Do you have any other suggestions for me?

  10. Rajesh says

    May 9, 2019 at 3:40 pm

    Dim olItem As Outlook.MailItem

    Compile Error: User defined type not defined, though I have set a reference to Outlook Object Library in Excel's Tools, References dialog. Is there anyother do i need look into it?

    Reply
    • Diane Poremsky says

      June 7, 2019 at 1:08 am

      That should be all you need. Does It error on a specific line?

      Reply
  11. Chris v says

    May 3, 2019 at 3:56 pm

    Hi - The formatting (links, hyperlinks, bold and color text, etc) in my template does not show up. Everything appears as unformatted richtext. How do I keep the formatting I setup in the template?

    Reply
  12. Chitra says

    April 15, 2019 at 3:05 am

    Hi,
    How to use the Word Object Library to update the fields. Please help

    Reply
    • Diane Poremsky says

      April 15, 2019 at 7:36 am

      I'm not sure exactly what you want to do or what fields you want to update, but the processs is similar - set a reference to the other object model and properly reference it. In most cases, you can use a macro from the other program with just a few tweaks.

      Reply
  13. Izzy says

    March 17, 2019 at 1:25 am

    Hi,

    Thank you very much for the above and it is working like a charm for me! I'm trying to change this code so that I can fill up some values in a table (That I created in the email template). When I run the Macro the email loses all the formatting including the table rows and columns and just fills the data one after the other. Is there a way to fix it? Thank you!

    Reply
    • Diane Poremsky says

      March 17, 2019 at 5:30 am

      You need to change body in these lines to htmlbody
      .Body = Replace(.Body, "[FirstName]", strFirstname)
      .Body = Replace(.Body, "[AcctMgrEmail]", AcctMgrEmail)
      .Body = Replace(.Body, "[strAcctMgrName]",

      Reply
      • Izzy says

        March 17, 2019 at 7:38 am

        Thank you Diane! It did work but there were a couple of things that I had to figure out. I had to spell check the email template and ignore all errors and save it before running the Macro! Apart from that need to make sure the template is saved in HTML format. That's it! Thank you again for your help!

  14. chhavi says

    November 27, 2018 at 10:40 am

    Hi,I have created a button in excel which on clicking send an email .But i want to make it much more convenient .Rather than opening Excel file every time and clicking the button,I want to create a macro in outlook mail which on clicking generate this email .Could it be done?

    Reply
    • Diane Poremsky says

      March 17, 2019 at 10:05 am

      Sorry l missed this earlier - yes you can create it from Outlook.

      Reply
  15. Wes says

    October 2, 2018 at 9:48 am

    Hi, When I use the VBA code, it doesn't use my template. Instead this displays an unrelated draft from my email account with the email addresses from the spreadsheet. Even after I deleted the draft, the macro still creates emails from that old draft.

    Reply
    • Diane Poremsky says

      March 17, 2019 at 10:08 am

      Did you change the code to use a different template ?
      Set olItem = olApp.CreateItemFromTemplate(appdata & "\Microsoft\Templates\macro-test.oft")

      Reply
  16. Shreekrishna says

    August 3, 2018 at 12:05 am

    Hi, am getting error as "user defined type not defined" - Compiler error at "Dim olItem As Outlook.MailItem".
    Could you please check

    Reply
    • Diane Poremsky says

      October 2, 2018 at 10:27 am

      Did you set a reference to Outlook Object Library in Excel's Tools, References dialog? (I'll add the steps to this page.)

      Reply
  17. angela says

    June 19, 2018 at 5:19 pm

    thank you this is helpful. what do I put in the "enviro = CStr(Environ("USERPROFILE"))" is this the path on my C drive of my user profile?

    Reply
    • Diane Poremsky says

      June 20, 2018 at 12:36 am

      Yes, that is the path to your user profile - c:\users\yourname - I use it in the macros because it will use the correct folder on every computer. If the workbook is saved elsewhere, you can replace this with the correct path:
      strAttachPath = enviro & "\Documents\Send\"

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 31 Issue 7

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • Reset the New Outlook Profile
  • Disable "Always ask before opening" Dialog
  • This operation has been cancelled due to restrictions
  • How to Hide or Delete Outlook's Default Folders
  • Change Outlook's Programmatic Access Options
  • Use Public Folders In new Outlook
  • Removing Suggested Accounts in New Outlook
  • How to Delete Stuck Read Receipts
  • Sync Issues and Errors with Gmail and Yahoo accounts
  • Error Opening iCloud Appointments in Classic Outlook
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Sync Issues and Errors with Gmail and Yahoo accounts

Error Opening iCloud Appointments in Classic Outlook

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.