VBA & Macro Problems

Word VBA: Copy Content from a Hidden Source Document

Khadija KhanKhadija Khan Nov 20, 2026 868 views

Question details

Copy formatted content from a hidden source document into another Word document without relying on changing document focus.

Copy Content from a Hidden Source Document in Word VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

With the destination document active, open Developer > Visual Basic. In the editor, choose Insert > Module.

2
Add the macro

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

3
Run the macro

Return to the destination document and run CopyFromHiddenDocument from Developer > Macros. Check the end of the document for the copied content.

Use Document and Range Variables to Copy Formatted Content
Choose the destination range carefully: Assigning FormattedText replaces the contents of the destination range. The example collapses the range first so that the source content is inserted instead of replacing the destination document.
Free Microsoft Office alternative

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.

Open and edit Microsoft Word DOC and DOCX filesWork with formatted document content in WPS WriterUse a familiar interface for everyday document editingMove existing documents into a lightweight office suite
microsoft office alternative - wps office

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.