• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
    • Common Problems
    • Outlook BCM
    • Utilities & Addins
    • Video Tutorials
    • EMO Archives
    • Outlook Updates
    • Outlook Apps
    • Forums

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     4 Comments

May 19, 2018 by Diane Poremsky 4 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

Related Posts:

  • Send Email to Addresses in an Excel Workbook
  • Log Messages and Attachment Names
  • Macro to Export Outlook Fields to Excel
  • Use a Macro to Copy Data in an Email 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.

4
Leave a Reply

2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 
2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 

  Subscribe  
newest oldest most voted
Notify of
Shreekrishna
Shreekrishna

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

Vote Up00Vote Down Reply
August 3, 2018 12:05 am
Diane Poremsky
Diane Poremsky

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

Vote Up00Vote Down Reply
October 2, 2018 10:27 am
angela
angela

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?

Vote Up00Vote Down Reply
June 19, 2018 5:19 pm
Diane Poremsky
Diane Poremsky

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\"

Vote Up00Vote Down Reply
June 20, 2018 12:36 am

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

Latest EMO: Vol. 24 Issue 3

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?

Subscribe to Exchange Messaging Outlook






Our Sponsors

  • Popular
  • Latest
  • Week Month All
  • Adjusting Outlook's Zoom Setting in Email
  • The Signature or Stationery and Fonts button doesn't work
  • Security Certificate Warning in Microsoft Outlook
  • This operation has been cancelled due to restrictions
  • How to Remove the Primary Account from Outlook
  • Two Copies of Sent Messages in Outlook
  • iCloud error: Outlook isn't configured to have a default profile
  • Outlook's Rules and Alerts: Run a Script
  • Outlook is Not Recognized as the Default Email Client
  • To Cc or Bcc a Meeting Request
  • Outlook.com: Manage Subscriptions
  • Group By Views don’t work in To-Do List
  • Category shortcuts don’t work
  • How to disable the Group By view in Outlook
  • Adjusting Outlook's Zoom Setting in Email
  • Change the Subject of an Incoming Message
  • Creating Signatures in Outlook
  • Scheduling a Recurring Message
  • OneNote is missing from Office 365 / 2019
  • Create Rules using PowerShell
Ajax spinner

Newest VBA Samples

Adjusting Outlook's Zoom Setting in Email

Move email items based on a list of email addresses

Remove prefix from Gmail meeting invitations

How to hide LinkedIn, Facebook, Google and other extra contact folders in Outlook.com

Use VBA to create a Mail Merge from Excel

Open multiple Outlook windows when Outlook starts

Set most frequently used Appointment Time Zones

How to change the From field on incoming messages

VBA: File messages by client code

Update Contact Area Codes

Recent Bugs List

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

Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Better Outlook Reminders?
  • Coming Soon to Windows 10: Office 365 Search
  • Outlook Links Won’t Open In Windows 10
  • BCM Errors after Upgrading to Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook 2016 Top Issues

  • The Windows Store Outlook App
  • Emails are not shown in the People Pane (Fixed)
  • Calendars aren’t printing in color
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • BCM Errors after October 2017 Outlook Update
  • Excel Files Won’t Display in Reading Pane
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

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

Outlook-tips.net Samples

VBOffice.net samples

OutlookCode.com

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

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
  • “Live” Group Calendar Tools

Convert to / from Outlook

  • Converting Messages and Calendar or
    Address books
  • Moving Outlook to a New Computer
  • Moving Outlook 2010 to a new Windows computer
  • Moving from Outlook Express to Outlook

Recover Deleted Items

  • Recover deleted messages from .pst files
  • Are Deleted Items gone forever in Outlook?

Outlook 2013 Absolute Beginner's Guide

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

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

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Sync & Share

Share Calendar & Contacts

Synchronize two machines

Sharing Calendar and Contacts over the Internet

More Tools and Utilities for Sharing Outlook Data

Access Folders in Other Users Mailboxes

View Shared Subfolders in an Exchange Mailbox

"Live" Group Calendar Tools

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

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

You are going to send email to

Move Comment