Run a Word VBA Macro on Each Mail Merge Section
Question details
Process individual documents in a Word mail merge output using a VBA macro.

- 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.
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.
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.
With the merged document open in Word, select Developer > Visual Basic, then choose Insert > Module.
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
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.
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.

Run one find and replace across the merged document
Use a document-wide replacement instead of a section loop when every section needs the same text change.
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.

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.




