How to Add File Names to Footers in Multiple Word Documents via VBA
Question details
The user wants to automate the process of inserting the file name into the footers of multiple Word documents within a folder and its subfolders using VBA.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Batch processing multiple Word documents in a directory and its subfolders to dynamically update their footers.
- Observed behavior
- The user needs the correct VBA syntax to insert dynamic file names instead of static text and requires a way to save the processed files without triggering repetitive 'Save As' prompts.
Before running the batch macro, ensure that none of the target documents are set to 'Read-only' to prevent unexpected 'Save As' prompts from halting the execution.
Use the Fields.Add Method in a Recursive VBA Script
Replace the TypeText method with Fields.Add to insert a dynamic file name field, allowing seamless batch processing across entire directories.
To properly insert a file name that updates dynamically, you cannot use the TypeText method. Instead, you must use the Fields.Add method with the wdFieldFileName constant. This instructs Word to insert the specific field type for the document's name.
To process documents in multiple folders, wrap this action within a recursive VBA procedure using the FileSystemObject (FSO) or Dir function to loop through all files and subfolders.
Press Alt + F11 to launch the Visual Basic for Applications (VBA) editor in Word.
Click 'Insert' from the top menu, then select 'Module' to create a blank script window.
Within your footer-editing block, use the following syntax to insert the file name: Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldFileName.
Ensure your recursive loop saves and closes each file automatically by appending ActiveDocument.Close SaveChanges:=wdSaveChanges at the end of the document processing block.

Batch Process Documents with WPS Office VBA
WPS Office Writer fully supports VBA macros, allowing you to seamlessly execute scripts that batch-insert file names into footers across hundreds of documents without compatibility issues.
- 1. Download and Install WPS Office: Get the latest version of WPS Office and open WPS Writer.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon.
- 3. Open the VBA Editor: Click 'Macros' or 'Visual Basic' to launch the integrated VBA environment.
- 4. Paste the Recursive Script: Insert a new module and paste your folder-looping macro utilizing the Fields.Add method.
- 5. Run the Batch Process: Execute the macro to automatically process folders, subfolders, and update footers in all your files.

Frequently Asked Questions
Why am I getting a 'Save As' prompt for each document when the macro runs?
This typically occurs if the documents in your folder are configured to open as read-only, or if they are locked by another process. Right-click your parent folder in File Explorer, select Properties, and uncheck the 'Read-only' attribute to fix this.
Why shouldn't I use the TypeText method for inserting file names?
The TypeText method inserts static text based on the active document at the exact moment the macro runs. Using the Fields.Add method with wdFieldFileName inserts a dynamic field that correctly reflects the current file's name and updates if the file is renamed.
How can I make my macro process documents inside subfolders?
You need to implement a recursive VBA procedure. By utilizing the FileSystemObject (FSO), your macro can loop through all document files in the parent directory, and then recursively call the same function for every subfolder it discovers.




