logo
search
VBA & Macro Problems

How to Combine More Than 200 Word Documents Automatically Using VBA

Huda QurayshiHuda Qurayshi Oct 23, 2026 868 views

Question details

The user needs a method to automatically merge over 200 separate Word documents into a single file while maintaining specific document elements.

How to Combine More Than 200 Word Documents Automatically Using VBA
Product
Word
Device & OS
not provided
Scenario
Merging a massive batch of Word files into one master document, requiring automated insertion of source filenames into headers, continuous page numbering, and a functional Table of Contents.
Observed behavior
When manually combining documents, filename fields automatically update to the destination document's name, losing the original file identifiers in the headers.
Before you start

Gather all the Word documents you intend to merge into a single, dedicated folder on your computer to simplify the batch process, and ensure you have enabled the Developer tab to access VBA features.

Solution 1Recommended

Run a VBA Batch Process Macro to Combine Documents

Use a custom VBA script to automatically loop through a folder, extract filenames, convert them to plain text headers, and merge the contents into a master document.

A VBA macro is the most efficient way to handle 200+ documents. The key to preserving the original filenames in the header is to have the script unlink the filename fields so they become ordinary text before the merge. Otherwise, the field will dynamically update to match the name of your new master document.

1
Open the VBA Editor

Open a new, blank Word document. Press ALT + F11 to launch the Microsoft Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click 'Insert' in the top menu and select 'Module'. A blank code window will appear.

3
Paste the Batch Merge Code

Paste a VBA script designed for batch processing. Ensure the code includes instructions to insert the filename as a Heading 1 or TC field, unlink header fields, and append the document contents.

4
Run the Macro

Press F5 or click the 'Run' button. When prompted, select the folder containing your 200+ documents and let the script process the batch.

5
Generate the Table of Contents

Once the merge is complete, go to the top of your master document, navigate to the 'References' tab, and click 'Table of Contents' to generate a hyperlinked TOC based on the heading styles inserted by the macro.

Run a VBA Batch Process Macro to Combine Documents
Unlinking Fields: Unlinking the filename field before combining is crucial. This locks the text so it continues to display the source document's name rather than updating to the master file's name.

Effortlessly Manage and Merge Documents with WPS Office

WPS Writer provides a robust, user-friendly environment for handling extensive document merges. With its advanced object insertion features and professional VBA macro support, combining hundreds of documents and creating structured Tables of Contents is fast and reliable.

  1. 1. Open a Blank Document: Launch WPS Writer and create a new blank document to serve as your master file.
  2. 2. Insert Text from Files: Navigate to the 'Insert' tab, click the 'Object' dropdown, and select 'Text from File'.
  3. 3. Select Documents to Merge: Highlight the documents you wish to combine from your folder and click 'Insert' to merge them sequentially.
  4. 4. Add a Table of Contents: Go to the 'References' tab, click 'Table of Contents', and select your preferred style to automatically generate page navigation links.
Fully compatible with Microsoft Word formats (.doc, .docx)Supports advanced VBA macros for powerful document automationIntuitive References tab for easy Table of Contents generationLightweight architecture handles large, 200+ page documents smoothly
microsoft office alternative - wps office

Frequently Asked Questions

Why do all my merged headers show the master document's filename?

This happens because the Filename field in Word automatically updates to reflect the current document. To prevent this, you must convert the filename field to ordinary text (unlink the field) before combining the documents.

What is the difference between a Table of Contents and an Index in Word?

A Table of Contents is built from Heading styles and provides a sequential list of chapters or sections with clickable page links. An Index, however, lists specific keywords or topics alphabetically and does not serve as structural navigation.

How do I ensure continuous page numbers across my merged documents?

After merging, double-click the footer to open the Header & Footer tools. Go to 'Page Number' -> 'Format Page Numbers', and ensure 'Continue from previous section' is selected.

Can I use VBA to add filenames as Table of Contents entries?

Yes, your VBA batch script can be programmed to insert the unlinked filename at the very beginning of each document and format it with a 'Heading 1' style or insert a 'TC' field. The Table of Contents will then pick these up automatically.