Word VBA can automate a mail merge by telling a prepared Word merge document to create a new document containing the merged records. Set up the recipients and merge fields in desktop Word first, then run a short macro; the merge result still depends on a correctly formatted data source and a correctly prepared main document.
What Word VBA mail merge means
VBA, or Visual Basic for Applications, is the programming language built into desktop Microsoft Office apps. In Word, it can control the Mail Merge features you would otherwise operate through the ribbon. A macro can run a merge, choose its output destination, apply document-specific steps, and—in a more developed workflow—repeat those steps for different groups of recipients.
VBA does not replace the mail merge itself. Word still combines a main document, such as a letter, with a recipient list containing fields such as first name, address, or account number. The macro automates actions around that process. This is useful when a merge is repeated regularly or when a person needs to produce the same kind of personalized document in a consistent way.
Mail merge is not limited to letters. Word can also create labels and envelopes, and it can prepare email messages using a compatible desktop email setup. The steps and output options differ, so a macro written to create a merged document should not be assumed to send email or print labels.
How Word mail merge works
A typical merge has three parts: a main document, a data source, and merge fields. The main document contains the shared text. The data source supplies one row per recipient, with column headings such as FirstName and City. Merge fields mark where Word should insert values from those columns. During the merge, Word makes a personalized copy for each selected row.
For example, a spreadsheet might have columns named FirstName, Street, City, and PostalCode. A letter can include «FirstName» in its greeting and address fields in its inside address. If the spreadsheet has 25 selected rows, a merge to a new document can produce 25 personalized letters. A blank cell remains blank; it does not automatically create a suitable phrase or remove surrounding punctuation.
VBA can start the merge, but it cannot reliably compensate for unclear headings, inconsistent values, or fields that were inserted incorrectly. It is usually easier to prepare and preview the merge using Word’s Mailings tools, then automate the repeatable final action.
Prepare the data and main document
Before writing or running a macro, make sure the ordinary merge works manually. In Excel, use one clear header row, keep each recipient on its own row, and avoid merged cells in the list. Store one type of information in each column. For instance, put city and postal code in separate columns rather than combining a whole address into a single value if you need to format those parts independently.
Save the workbook in a format Word can read, such as .xlsx, and close it before connecting if Word has trouble accessing it. In Word for Microsoft 365 on Windows, start a letter through Mailings > Start Mail Merge > Letters. Choose Mailings > Select Recipients > Use an Existing List and select the workbook and relevant sheet or named range. Then use Insert Merge Field to place the required fields in the document.
- Check that the selected worksheet or range includes the header row and the intended recipient rows.
- Use Mailings > Preview Results to inspect several records, including records with longer names or missing optional details.
- Use Mailings > Edit Recipient List to confirm that the right recipients are selected.
- Save the main Word document after setting up its merge connection. A .docm file is needed if you want the VBA code stored in that document.
The Mailings tab and merge concepts are also available in Word for Mac, although dialog layout and VBA editing access can differ. Word for the web can display and edit many Word documents, but it does not run VBA macros; use the desktop app to execute one.
Run a prepared merge with VBA
The following Word macro uses the active document as the main merge document. It checks that the document is set up as a merge, sends the output to a new Word document, and executes the merge using the recipient selection already configured in Word. It does not connect a workbook, choose a recipient filter, save the result, print it, or send email.
Sub CreateMergedDocument()
Dim mainDoc As Document
' Keep the prepared merge document as the source document.
Set mainDoc = ActiveDocument
If mainDoc.MailMerge.MainDocumentType = wdNotAMergeDocument Then
MsgBox "The active document is not a mail merge main document."
Exit Sub
End If
With mainDoc.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.Execute Pause:=False
End With
End Sub
To add it in Word for Windows, open the prepared document, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste the macro into the module. Return to Word and run it from View > Macros > View Macros by selecting CreateMergedDocument and choosing Run. Depending on ribbon configuration, the Macros command may be shown under the View or Developer tab. On Mac, open the Visual Basic Editor from Tools > Macro > Visual Basic Editor; keyboard shortcuts can vary by keyboard and system settings.
When the macro completes, Word creates a separate merged document. Review it and save it with a meaningful name using File > Save As. The source main document remains the template for the merge. If you want to change the recipients, fields, or layout, make those changes in the source document and data list rather than assuming the generated output will update automatically.
Choosing output and extending the automation
The macro uses wdSendToNewDocument, which is appropriate when you want to inspect, edit, or save the combined result. Other Word workflows can send merged output to a printer or create email messages, but the intended destination should be selected deliberately. For example, a large batch sent straight to a printer is harder to review than a merged document saved first.
A more advanced macro can set a first and last record, merge subsets, save files, or export documents. These additions require careful decisions about file names, duplicate names, output folders, and what to do when a merge fails. If each recipient must receive a separate PDF, for example, a single merged document is not the same as a folder of individually named PDFs. That workflow needs additional VBA to identify record boundaries and save each output safely.
For email merges, the Word interface and a compatible desktop email configuration are important. A document merge macro that creates a new Word document does not send messages. Before sending any personalized email, inspect the recipient list, message content, and chosen account, and test with a small, controlled recipient set.
Common problems and practical fixes
- The macro says the document is not a merge document. The active file may be the output rather than the main document, or the main document may not have been set up with Mailings > Start Mail Merge. Open the prepared source file and check its merge settings.
- Word cannot find the spreadsheet or fields. Reconnect the data source with Mailings > Select Recipients, confirm the worksheet or range, and verify that the header names match the fields inserted into Word. Moving or renaming a source file can break a saved connection.
- Some values look different from Excel. Dates, leading zeros, and numeric formats can be interpreted differently by Word depending on the connection and data. Store identifiers such as postal codes as text when leading zeros matter, and preview records before producing the full batch.
- There are blank lines or awkward punctuation. A blank merge field does not automatically fix the rest of a sentence or address. Use Word’s merge rules where appropriate, adjust the document layout, and test records with empty optional fields.
- The macro is blocked or does nothing. The file may be a .docx, which cannot store VBA code, or Office security settings may block macros. Save code-bearing documents as .docm and follow your organization’s macro policy; do not enable macros from an untrusted file just to make it run.
Macro safety, saving, and compatibility
VBA can change documents and perform actions that are difficult to reverse. Save a copy of the source workbook and the main Word document before running a macro. Macro actions generally cannot be undone with Ctrl+Z, so inspect the output before replacing or deleting source files. Keep personal or confidential recipient data in approved storage, and avoid sharing a macro-enabled file with data embedded in it unless recipients are authorized to see that data.
Use only code you understand or have reviewed, and keep the macro limited to the task it needs to perform. Office may show a security warning for a macro-enabled file. Follow your organization’s instructions or Microsoft’s security guidance rather than lowering macro security broadly. The code above relies on Word’s built-in mail merge object model and does not need Excel VBA to run, though Word still needs access to the connected source.
When to use VBA—and alternatives
VBA makes sense when the same merge is repeated, when a defined sequence must be followed every time, or when a manual operation is part of a larger Office workflow. For a one-off letter, the Mailings ribbon is often simpler and easier to troubleshoot. A macro adds maintenance: someone must preserve the code, check compatibility, and update it when the document or source list changes.
For a small merge, use Word’s built-in Mailings commands and save the result manually. For recurring work involving many separately named files or complex rules, consider whether a managed document-generation service or an organization-approved workflow tool is more suitable. Those alternatives can involve licensing, setup, and data-handling requirements; they are not automatically safer or simpler than a small Word macro.
Frequently Asked Questions
Can VBA create a Word mail merge automatically?
Yes. Word VBA can run a merge through the document’s MailMerge object. The main document and recipient data source still need to be prepared or connected, unless the macro also contains code to set them up.
Can I use a macro to merge Excel data into Word?
Yes. Set Word’s recipient list to an Excel workbook, insert the corresponding merge fields, and run the macro from the prepared Word main document. The sample macro assumes that this connection and recipient selection already exist.
Does the macro send emails?
No. The example creates a new merged Word document. Sending email is a separate merge destination and requires a compatible desktop email setup, a checked recipient list, and code or interface steps designed for that purpose.
Can Word for the web run a mail merge macro?
No. Word for the web does not run VBA. Open the macro-enabled document in the desktop Word application to run its code.
Why does my macro not appear in the Macros list?
Check that the code is in a VBA module in the open document or an available template, that the document is saved in a macro-enabled format when it stores the code, and that macro security has not blocked it. Organization-managed settings may restrict macros.
Related
- Double Space in Word: Easy Steps for Desktop and Web
- IFS Syntax in Google Sheets: Examples and Common Errors
- IF vs IFS in Google Sheets: Which Formula Should You Use?
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Button: Add, Assign, and Fix Macro ButtonsAn Excel VBA button is a clickable control that runs a macro in a workbook. In Excel for…
- How to Delete Blank Rows in Excel with a VBA MacroTo delete blank rows in Excel with a macro, select the data range, run a VBA procedure that…
- Excel UserForm ComboBox: Load a List with VBATo load an Excel UserForm ComboBox from worksheet values, add a ComboBox to the form, then fill it…
- Copy and Paste Values in VBA: Methods and ExamplesTo copy only cell values with Excel VBA, assign one range’s Value2 to another range of the same…
- Delete Entire Row VBA: Examples and Safety TipsTo delete a complete worksheet row with VBA, use Rows(rowNumber).Delete on a specifically named worksheet, such as Worksheets("Data").Rows(5).Delete.…
- VBA Loop Structures: How to Choose and Use Each TypeVBA has three main loop structures: For...Next for a known number of repetitions, For Each...Next for processing items…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.