Running the same VBA macro on every Word document inside a folder saves hours of manual work. You can apply formatting changes, insert headers, or update tables across hundreds of files without opening each one individually. This article explains how to write and execute a VBA script that loops through all .docx files in a specified folder and runs your macro on each document. You will learn the exact code structure, how to set the folder path, and what to do when Word blocks the macro or when a file is corrupted.
Key Takeaways: Batch Macro Execution for Multiple Documents
- Dir function with .docx wildcard: Loops through all Word files in a folder without needing a file dialog.
- Documents.Open method inside the loop: Opens each file, applies the macro, then closes and saves changes automatically.
- Application.ScreenUpdating = False: Speeds up the batch process by hiding the document window during execution.
How the VBA Folder Loop Works and What You Need Before Starting
VBA uses the Dir function to find all files that match a pattern such as “docx” inside a folder path you specify. The script opens each file using Documents.Open, runs your custom code on the active document, then closes the file with Close SaveChanges:=wdSaveChanges. This loop repeats until no more matching files exist.
Before you run this macro, you must enable the Developer tab in Word. Go to File > Options > Customize Ribbon and check Developer in the right panel. You also need to set the macro security level to allow digitally signed macros or enable all macros temporarily. Go to Developer > Macro Security and select Enable all macros. Change this back after running the batch macro to protect your system.
The folder path in the macro must use double backslashes or forward slashes. Example: C:\\Users\\YourName\\Documents\\Reports\\. The folder must contain only the documents you want to process. Make a backup copy of the folder before running the macro for the first time.
Steps to Create and Run a VBA Macro That Processes All Documents in a Folder
- Open the VBA Editor
Press Alt+F11 in Word. This opens the Microsoft Visual Basic for Applications window. - Insert a new module
In the Project Explorer pane, right-click Normal or your current document. Select Insert > Module. A blank code window appears. - Paste the folder loop template code
Copy and paste the following code into the module window:Sub ProcessAllDocsInFolder() Dim folderPath As String Dim fileName As String Dim doc As Document ' Change this path to your target folder folderPath = "C:\Users\YourName\Documents\Reports\" fileName = Dir(folderPath & "docx") Application.ScreenUpdating = False Do While fileName <> "" Set doc = Documents.Open(folderPath & fileName) ' Your custom macro code goes here ' Example: change all headings to bold ' ActiveDocument.Content.Font.Bold = True doc.Close SaveChanges:=wdSaveChanges fileName = Dir() Loop Application.ScreenUpdating = True End Sub - Edit the folder path
Replace the path infolderPath = "..."with the actual folder location on your computer. Use double backslashes between folder names. - Add your macro code between Open and Close
Replace the comment line with the actual VBA code you want to run on each document. For example, to add a company watermark, insert:With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) .Shapes.AddTextEffect msoTextEffect1, "CONFIDENTIAL", "Arial", 36, _ msoFalse, msoFalse, 0, 0 End With - Run the macro
Press F5 while the cursor is inside theProcessAllDocsInFoldersub. Word will open each file, apply your code, save the changes, and close the file. The screen will not flicker becauseScreenUpdatingis turned off. - Check the results
Open a few documents from the folder to verify the macro applied the changes correctly. If the changes are wrong, restore from your backup and adjust the code.
Alternative Method: Use a File Dialog to Select the Folder
If you prefer not to hard-code the folder path, use the FileDialog object. This lets the user pick a folder each time the macro runs. Add this code before the Dir line:
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
If fd.Show = -1 Then
folderPath = fd.SelectedItems(1) & "\"
Else
Exit Sub
End If
This approach works well when you share the macro with colleagues who store documents in different locations.
Common Problems When Running a Macro on All Documents in a Folder
Word Blocks the Macro with a Security Warning
If the macro does not run and Word shows a security notice, the file is from an untrusted location. Move the documents to a folder you trust. Go to File > Options > Trust Center > Trust Center Settings > Trusted Locations. Add the folder path to the list. Word will then allow macros to run on files in that folder.
The Macro Stops After an Error in One File
A corrupted document or a file opened in Protected View can break the loop. Add error handling inside the loop to skip problematic files. Insert this line after Set doc = Documents.Open(...):
On Error Resume Next
Place On Error GoTo 0 after the Close line to restore normal error handling. This allows the macro to continue with the next file even if one fails.
The Macro Does Not Process .docm or .dotx Files
The pattern "docx" only matches standard Word documents. To include macro-enabled documents, change the pattern to "docm". For templates, use "dotx" or "dotm". You can also loop through multiple patterns by nesting the Dir call inside a For Each loop over an array of extensions.
VBA Folder Loop vs Manual Open and Apply
| Item | VBA Folder Loop | Manual Open and Apply |
|---|---|---|
| Time per 100 documents | 2 to 5 minutes depending on macro complexity | 60 to 90 minutes |
| Consistency | Same code runs on every file without human error | Risk of skipping steps or applying changes inconsistently |
| Error handling | Can skip corrupted files automatically | Requires manual troubleshooting for each error |
| Setup effort | Write the macro once | Repeat the same steps for each document |
| Flexibility | Easy to modify the macro to handle subfolders or different file types | No automation possible |
Now you can automate repetitive formatting or content tasks across an entire folder of Word documents. Start by testing the macro on a small set of backup files. Once the code works correctly, apply it to the full folder. Use the On Error Resume Next statement to handle unexpected file errors without stopping the batch process.