Replace Word Footers in Folders and Subfolders Using VBA
Question details
Replace footers in Word documents in a selected folder and all its subfolders.

- 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.
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.
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.
In desktop Word, press Alt+F11, then select Insert > Module. Make sure macros are permitted by your organization's security settings.
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
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.

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.

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.




