How to Run Mail Merge From Macro in Word
🔍 WiseChecker

How to Run Mail Merge From Macro in Word

You need to automate a mail merge in Word without clicking through the ribbon every time. Running a mail merge from a macro lets you generate hundreds of personalized letters, emails, or labels with one command. This article explains how to write and execute a VBA macro that performs a full mail merge from start to finish. You will learn the required objects, the step-by-step code structure, and how to avoid common automation errors.

Key Takeaways: Automating Mail Merge With VBA Macros

  • MailMerge object and MainDocumentType: Set the merge type to letters, emails, or labels before running the merge.
  • OpenDataSource method: Point the macro to your Excel or Access data source file using the full file path.
  • Execute method: Perform the merge to a new document, printer, or email without showing the wizard.

ADVERTISEMENT

What the Mail Merge Object Model in Word VBA Does

Word VBA exposes the mail merge feature through the MailMerge object, which is a child of the Document object. This object controls every aspect of a merge: the main document type, the data source, the merge fields, and the output destination. Before you write any code, you must understand three core elements.

The first element is the MainDocumentType property. It tells Word what kind of merge you are performing. The options include wdFormLetters, wdMailingLabels, wdEnvelopes, wdCatalog, and wdEMail. Setting this property correctly ensures Word builds the correct output structure.

The second element is the data source. The OpenDataSource method connects your main document to an external file. This file can be an Excel workbook, an Access database, a CSV file, or an Outlook contacts folder. You must supply the full path to the file. If the data source has multiple sheets or tables, you also specify the SQLStatement parameter to select the correct range.

The third element is the Execute method. This method runs the merge and sends the output to one of several destinations: a new document (wdSendToNewDocument), the printer (wdSendToPrinter), or email (wdSendToEmail). You can also suppress the prompts by setting ShowWizard to False and SuppressBlankLines to True.

Prerequisites for Running a Mail Merge Macro

You need a main document that already contains merge fields. The macro does not insert fields for you. Open the main document in Word and insert the fields using the Mailings ribbon or by typing «FieldName» manually. The data source must be accessible at the file path you specify in the macro. If the data source is an Excel file, use a named range or a sheet reference such as Sheet1$.

Enable macros in Word before running the code. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros. This is not a secure setting for production environments, so disable it after testing.

Steps to Write and Run a Mail Merge Macro in Word

The following macro performs a mail merge to a new document using an Excel data source. Adjust the file paths and sheet names to match your environment.

  1. Open the VBA Editor
    Press Alt+F11 in Word. The Visual Basic for Applications editor opens. If the Project Explorer is not visible, press Ctrl+R to show it.
  2. Insert a new module
    In the Project Explorer, right-click Normal or your current document project. Choose Insert > Module. A blank code window appears.
  3. Paste the macro code
    Copy and paste the following code into the module window:
    Sub RunMailMergeToNewDoc()
        Dim mainDoc As Document
        Set mainDoc = ActiveDocument
        
        With mainDoc.MailMerge
            .MainDocumentType = wdFormLetters
            .OpenDataSource _
                Name:="C:\Data\Customers.xlsx", _
                SQLStatement:="SELECT  FROM [Sheet1$]", _
                SubType:=wdMergeSubTypeWord2000
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            .Execute Pause:=False
        End With
        
        Set mainDoc = Nothing
    End Sub
    

    Replace C:\Data\Customers.xlsx with the full path to your data source. Replace [Sheet1$] with the correct sheet name or named range.

  4. Run the macro
    Press F5 while the cursor is inside the macro. Word connects to the data source, merges every record, and creates a new document containing the merged letters.
  5. Save the merged document
    The new document is unsaved. Press Ctrl+S to save it with a new name. The original main document remains unchanged.

Modifying the Macro for Email or Printer Output

To send the merge output directly to the printer, change the Destination line to .Destination = wdSendToPrinter. To send as email, change it to .Destination = wdSendToEmail and add the following lines before .Execute:

.MailSubject = "Your subject line here"
.MailAddressFieldName = "Email"

The MailAddressFieldName must match the column name in your data source that contains the email addresses.

ADVERTISEMENT

Common Automation Errors and How to Fix Them

Word Cannot Find the Data Source File

The macro fails with error 5152 or a message that the file cannot be opened. This occurs when the file path in the OpenDataSource method is incorrect or the file is locked by another process. Verify the path exists and the file is not open in Excel. Use absolute paths only. Avoid paths with spaces unless you wrap the entire path in quotes.

The Merge Runs but Produces Blank Documents

Blank output usually means the merge fields in the main document do not match the column names in the data source. Check the field names by pressing Alt+F9 in the main document to reveal field codes. For example, { MERGEFIELD FirstName } must match a column named FirstName in the data source. Case does not matter, but spelling must be exact.

Macro Stops With a Permission Error

Word may block the macro if the document is in a trusted location or if macro security is set too high. Move the main document to a trusted folder. Go to File > Options > Trust Center > Trust Center Settings > Trusted Locations. Add the folder containing your main document. Alternatively, digitally sign the macro to run it without lowering security.

Manual Mail Merge vs Macro-Driven Mail Merge

Item Manual Mail Merge Macro-Driven Mail Merge
Setup time Requires clicking through 5+ ribbon steps One click after initial VBA setup
Repeatability Must repeat the wizard for each new data set Same macro works for any data source
Error handling Word shows dialog boxes for missing data Macro stops or skips records silently
Output flexibility Choose destination each time Destination is hard-coded in macro
Skill required No programming knowledge Basic VBA understanding needed

Manual mail merge works well for occasional use. Macro-driven merge saves time when you run the same merge weekly or daily. The macro approach also lets you combine multiple merge steps into one automated process, such as generating letters and then saving each as a separate PDF.

You can now write a VBA macro that opens a data source, sets the merge type, and executes the merge to a new document, printer, or email. Test the macro with a small data set first to confirm field mapping. For advanced automation, explore the MailMergeDataFields collection to dynamically check which fields exist before running the merge. This prevents runtime errors when the data source structure changes.

ADVERTISEMENT