Others

Run a Word VBA Macro on Each Mail Merge Section

Aamir Naveed AkramAamir Naveed Akram Nov 20, 2026 868 views

Question details

Process individual documents in a Word mail merge output using a VBA macro.

Run a Word VBA Macro on Each Mail Merge Section
Product
Microsoft Word
Device & OS
not provided
Scenario
A merged Word document contains individual outputs separated by section breaks, and the user wants to apply a macro to each one.
Observed behavior
The user wants the macro to address each merged section separately, or apply a single operation to the whole document when appropriate.
Before you start

Complete the mail merge to a new document and check that its section breaks correspond to the outputs you want to process. Make the merged document active before running the macro, and enable the Developer tab if it is not visible.

Solution 1Recommended

Loop through the merged document's sections with Word VBA

Use this approach when each section needs to be handled separately.

The loop addresses each section through objSection.Range. A section is not necessarily the same as a merged record if a record itself contains section breaks, so check the output structure first.

1
Open the VBA editor

With the merged document open in Word, select Developer > Visual Basic, then choose Insert > Module.

2
Add the section loop

Paste this example into the module: Sub ProcessMergeSections() Dim objSection As Section For Each objSection In ActiveDocument.Sections Debug.Print objSection.Range.Text ' Add your changes to objSection.Range here. Next objSection End Sub

3
Add your processing code

Replace the comment inside the For Each loop with the actions you need to perform on objSection.Range. The example prints each section's text to the VBA Immediate window without changing the document.

4
Run the macro

Return to the merged document, select Developer > Macros, choose ProcessMergeSections, and select Run. In the VBA editor, choose View > Immediate Window to see the example output.

Loop through the merged document's sections with Word VBA
Section ranges include breaks: A section range can include its section break. Take care when replacing or deleting range content so you do not unintentionally remove document structure.
Free Microsoft Office alternative

Edit Word Documents with WPS Writer

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its Microsoft Office format compatibility helps you move documents between applications, though it does not fix Word VBA macros or guarantee macro compatibility.

Edit merged document text in WPS WriterOpen and save Microsoft Word DOC and DOCX formatsWork with a familiar, easy-to-use interfaceUse a free, lightweight office suite for everyday documents
microsoft office alternative - wps office

Frequently Asked Questions

Does each Word section always represent one mail merge record?

Not necessarily. A merged record may contain its own section breaks. Inspect the merged document's breaks before treating each item in ActiveDocument.Sections as a separate record.

How can I see what the example macro prints?

In the VBA editor, select View > Immediate Window. Run ProcessMergeSections with the merged document active to print the text of each section there.

Do I need a loop to replace the same text in every section?

No. For the same replacement throughout the main document text, use Home > Replace > Replace All, or run a document-wide VBA Find operation.

Why does the macro need the merged document to be active?

ActiveDocument.Sections refers to the document currently active in Word. If another document is active, the loop will process that document's sections instead.

Can WPS Writer fix a Word VBA macro?

No. WPS Writer is an alternative for editing Word documents, but it does not fix Microsoft Word VBA issues, and macro compatibility is not guaranteed.