Use VBA to Save Each Mail Merge Page as a Separate Word Document
Question details
The user wants to split a multi-page mail merge document into separate Word files using a VBA macro, ensuring correct filenames and preserved formatting.

- Product
- Word
- Device & OS
- not provided
- Scenario
- After running a mail merge, the user needs to automatically save each resulting page or record as an individual document with page-specific filenames and intact headers/footers.
- Observed behavior
- When executing the macro, the user encounters Save As dialog prompts, lost formatting, or errors extracting filenames because of path issues and incorrect quotation marks.
Ensure your mail merge document is fully set up and that you have enabled Developer tools and macros in your Word application before running the VBA script.
Adjust the VBA Code for Path, Formatting, and Filename Extraction
Update your VBA macro to reference the source document's path, properly copy formatted text, and extract filenames using straight quotes.
To successfully automate splitting a mail merge document without manual interruptions, specific object references and string functions must be configured correctly in your macro script.
Replace ActiveDocument.Path with sourceDoc.Path in your VBA script to save newly created files directly in the source document's folder without triggering a Save As dialog.
Define a text range for the specific page being extracted and assign its FormattedText property directly to the new document. This guarantees that headers, footers, and text styles are maintained.
Make sure the characters surrounding your intended filenames in the source text are straight single quotes (like 'filename'). Smart or curly quotes will cause InStr and InStrRev functions to fail.

Utilize a Dedicated Mail Merge Add-in
If you prefer not to write or debug VBA code, use a dedicated mail merge add-in to automatically split documents based on record fields.
Perform Mail Merges and Manage Documents Efficiently with WPS Office
WPS Writer offers a highly compatible and user-friendly interface for managing mail merges, handling macros, and saving separate documents. You can execute VBA scripts or utilize built-in document splitting features with ease.
- 1. Start your mail merge: Open your master document in WPS Writer and navigate to the References tab to access the Mail Merge tools.
- 2. Connect data and insert fields: Connect your data source and insert the necessary merge fields into your document layout.
- 3. Open the VBA Editor: If you are using a custom script to split files, navigate to the Developer tab to access the built-in VBA editor.
- 4. Run your macro: Paste your updated VBA macro code, ensuring path and formatting adjustments are made, then run the script to generate separate customized documents.

Frequently Asked Questions
Why does my VBA macro prompt the Save As dialog for every page?
This usually happens if you use ActiveDocument.Path for a newly created document that hasn't been saved yet. Change the code to use sourceDoc.Path or explicitly define the folder directory so the macro can save the files automatically.
How do I keep the original formatting when splitting pages into new documents?
Instead of using standard copy and paste functions, create a specific text range for each page in your VBA code and assign its FormattedText property directly to the new document.
Why is my VBA code failing to extract the filename from the text?
String functions like InStr and InStrRev look for exact character matches. Check that your document uses straight single quotes instead of curved smart quotes around the text you intend to use as the filename.
Can I split mail merge documents into separate PDFs using VBA?
Yes, you can adapt your VBA macro to export each individual page or record as a PDF by utilizing the ExportAsFixedFormat method instead of the standard SaveAs2 command.




