VBA & Macro Problems

Replace Word Footers in Folders and Subfolders Using VBA

Adam DavisAdam Davis Nov 25, 2026 868 views

Question details

Replace footers in Word documents in a selected folder and all its subfolders.

Replace Word Footers in Folders and Subfolders Using VBA
Product
Microsoft Word VBA
Device & OS
not provided
Scenario
A user wants to apply the same footer text to Word documents throughout a folder hierarchy.
Observed behavior
The existing macro uses Dir and processes documents in one folder, but it does not process documents in nested subfolders.
Before you start

Use a desktop version of Word that supports VBA, and make sure you can edit and save the target documents. Test on a small set of copies first: replacing footer text removes existing footer content, including page-number fields.

Solution 1Recommended

Use a recursive VBA procedure to update Word footers

Process .doc and .docx files in a selected folder, then repeat the operation for each subfolder.

This example uses FileSystemObject to enumerate files and folders, avoiding the shared enumeration state of Dir. It updates existing primary, first-page, and even-page footers in each section.

The macro replaces the entire footer content with plain text. Linked footers may share content with a preceding section, and protected or read-only documents may stop the macro.

1
Open the VBA editor

In desktop Word, press Alt+F11, then select Insert > Module. Make sure macros are permitted by your organization's security settings.

2
Add the recursive macro

Paste the following code into the module. Replace "Your new footer text" with the text you want to use: Option Explicit Sub ReplaceFootersInTree() Dim picker As Object Dim folderPath As String Dim oldAlerts As Long Dim oldSecurity As Long Dim errorMessage As String Set picker = Application.FileDialog(4) ' Folder picker If picker.Show <> -1 Then Exit Sub folderPath = picker.SelectedItems(1) oldAlerts = Application.DisplayAlerts oldSecurity = Application.AutomationSecurity On Error GoTo Failed Application.DisplayAlerts = wdAlertsNone Application.AutomationSecurity = 3 ' Disable macros in opened files ProcessFolder folderPath GoTo CleanUp Failed: errorMessage = Err.Description CleanUp: Application.DisplayAlerts = oldAlerts Application.AutomationSecurity = oldSecurity If errorMessage <> "" Then MsgBox "Stopped: " & errorMessage, vbExclamation Else MsgBox "Footer replacement completed." End If End Sub Private Sub ProcessFolder(ByVal folderPath As String) Dim fso As Object, folder As Object Dim file As Object, child As Object Dim doc As Document Dim sec As Section Dim footer As HeaderFooter Dim ext As String Dim errorNumber As Long, errorMessage As String On Error GoTo Failed Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder(folderPath) For Each file In folder.Files ext = LCase$(fso.GetExtensionName(file.Name)) If (ext = "doc" Or ext = "docx") And Left$(file.Name, 2) <> "~$" Then Set doc = Documents.Open(FileName:=file.Path, AddToRecentFiles:=False) If doc.ReadOnly Then Err.Raise vbObjectError + 1209, , "Read-only file: " & file.Path For Each sec In doc.Sections For Each footer In sec.Footers If footer.Exists Then footer.Range.Text = "Your new footer text" Next footer Next sec doc.Save doc.Close SaveChanges:=wdDoNotSaveChanges Set doc = Nothing End If Next file For Each child In folder.SubFolders ProcessFolder child.Path Next child Exit Sub Failed: errorNumber = Err.Number errorMessage = Err.Description On Error Resume Next If Not doc Is Nothing Then doc.Close SaveChanges:=wdDoNotSaveChanges On Error GoTo 0 Err.Raise errorNumber, "ProcessFolder", errorMessage End Sub

3
Run and verify

In the VBA editor, place the cursor inside ReplaceFootersInTree and press F5. Select the parent folder in the folder picker, then open sample documents from the parent and nested folders to verify their footers.

Use a recursive VBA procedure to update Word footers
Check the scope before running: The macro changes .doc and .docx files only and stops if it encounters an error. Documents saved before an error remain changed. Avoid folder junctions or symbolic links that could create a recursive loop.
Free Microsoft Office alternative

Edit Word Document Footers with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for editing documents and their footers. Its Microsoft Word format support makes it easier to move existing files between office suites; it does not fix or replace the Word VBA macro above.

Edit document footers in WPS WriterOpen and edit Microsoft Word .doc and .docx filesWork in a familiar, easy-to-use interfaceUse a free, lightweight office suite
QA img-9

Frequently Asked Questions

Why doesn't my Dir loop process subfolders?

Dir lists entries in the folder specified by its current search; it does not automatically traverse child folders. A recursive procedure can visit each subfolder, while FileSystemObject avoids interfering with Dir's shared enumeration state.

Will this macro preserve page numbers in the footer?

No. The footer.Range.Text assignment replaces the whole footer, including page-number fields and formatting. Test on copies and modify the code if those elements need to be preserved.

Does the macro update first-page and even-page footers?

Yes. It loops through each section's footers and updates those that exist. Footers linked to a preceding section can share the same content.

What happens if a document is read-only or protected?

The macro may stop and display an error. Check the file's permissions or protection in Word, then rerun the macro on the remaining documents after resolving the issue.

Can the macro process .docm files?

Not as written. Its extension check includes only .doc and .docx. Add docm to that check only if you have tested the behavior and are authorized to process those files.