Word VBA: Copy Content from a Hidden Source Document
Question details
Copy formatted content from a hidden source document into another Word document without relying on changing document focus.

- Product
- Microsoft Word VBA
- Device & OS
- not provided
- Scenario
- A Word macro needs to open a source document invisibly and copy its content into a destination document.
- Observed behavior
- ActiveDocument can refer to a different document when focus changes, causing the macro to use the wrong document.
Open the destination document in Word and locate the source document's full file path. Make sure you can run trusted macros and edit the macro in the Visual Basic Editor.
Use Document and Range Variables to Copy Formatted Content
Use this approach to copy from an invisible source document without changing focus or using the clipboard.
Capture the destination document before opening the source. The example appends the source content at the end of the destination and closes the source without saving changes.
With the destination document active, open Developer > Visual Basic. In the editor, choose Insert > Module.
Paste this code into the module and replace the example path with the source document's full path: Sub CopyFromHiddenDocument() Dim DocA As Document Dim DocB As Document Dim DestRange As Range Set DocB = ActiveDocument Set DocA = Documents.Open(FileName:="C:\path\DocA.docx", Visible:=False) Set DestRange = DocB.Content DestRange.Collapse Direction:=wdCollapseEnd DestRange.FormattedText = DocA.Content.FormattedText DocA.Close SaveChanges:=wdDoNotSaveChanges End Sub
Return to the destination document and run CopyFromHiddenDocument from Developer > Macros. Check the end of the document for the copied content.

Edit Word Documents with WPS Office
WPS Office is a free, lightweight alternative for working with Word documents in a familiar interface. Open compatible files and continue editing as you migrate; Word VBA macros may require separate compatibility checks.

Frequently Asked Questions
Why should I avoid using ActiveDocument throughout the macro?
ActiveDocument refers to the document with focus, which can change while the macro runs. Store the intended destination in a Document variable before opening the source, then refer to that variable.
How do I open the source document without displaying it?
Use Documents.Open with Visible:=False, such as Set DocA = Documents.Open(FileName:="C:\path\DocA.docx", Visible:=False). Replace the example path with the actual source path.
Does FormattedText use the clipboard?
No. Assigning the source range's FormattedText to the destination range's FormattedText transfers the formatted content directly between ranges.
How can I avoid replacing existing destination content?
Set a Range to the intended insertion location and collapse it before assigning FormattedText. For example, use Set DestRange = DocB.Content followed by DestRange.Collapse Direction:=wdCollapseEnd.
How do I close the hidden document without saving changes?
After copying, use DocA.Close SaveChanges:=wdDoNotSaveChanges. For production macros, add error handling so the hidden document is also closed if an error occurs.




