Capture a Merged Document After a Word Mail Merge
Question details
The user needs to keep a reference to the document created by a Word mail merge for subsequent processing.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A VBA procedure merges a template into a new document, then closes the template and performs conditional operations on the output.
- Observed behavior
- Closing the template can activate a different open document, so the active document or a generated document name may not identify the merged output.
Open the Word VBA editor and locate the procedure that executes the mail merge. Identify the variable that refers to the original template and the Word application instance used by the procedure.
Store the New Mail Merge Document in a VBA Variable
Use a document reference captured immediately after the merge to keep processing the correct output, even if another document becomes active.
The template variable still refers to the original document after the merge. Capture the new active document before closing the template or activating another window.
In Word, open the VBA editor and find the code that runs the mail merge. In the following example, wddoc refers to the template and objapp refers to its Word application instance.
Declare wdActiveDoc as a Word.Document, set the destination to a new document, and capture it immediately: Dim wdActiveDoc As Word.Document wddoc.MailMerge.Destination = wdSendToNewDocument wddoc.MailMerge.Execute Set wdActiveDoc = objapp.ActiveDocument
After capturing the output, close wddoc if your procedure no longer needs it. For example, use wddoc.Close SaveChanges:=wdDoNotSaveChanges only if you intend to discard any unsaved template changes.
Use wdActiveDoc for subsequent operations instead of names such as Document3 or Form Letters3. If you need its window to be active for an interactive operation, run wdActiveDoc.Activate first.

Edit Merged Documents with WPS Writer
WPS Office is a free, lightweight Office alternative with a familiar interface. Open compatible Word documents in WPS Writer for a straightforward move between office suites; it does not fix Microsoft Word VBA behavior.

Frequently Asked Questions
Why does Word activate a different document when I close the template?
Word may return to another recently used document when the template closes. Capture objapp.ActiveDocument immediately after the merge and use that stored reference instead of relying on which window is active later.
Does the template variable point to the merged document after execution?
No. The variable used for the original template still points to that document. Assign the merged output to a separate Word.Document variable immediately after MailMerge.Execute.
Can I use the generated document name to find the merge output?
Names such as Document3 or Form Letters3 can vary depending on the documents already open. A document object reference is more reliable than a generated name or document number.
How can I make the merged document active again?
After storing it in a variable such as wdActiveDoc, run wdActiveDoc.Activate. For operations that support a document reference, use wdActiveDoc directly instead of depending on the active window.




