Replace Text in Word Headers and Footers Using VBA
Question details
Replace specified text in headers and footers throughout a Word document using VBA.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- An invoice number or other repeated text needs to be changed across multiple document sections and header or footer types.
- Observed behavior
- The desired result is for matching text in existing primary, first-page, and even-page headers and footers to be replaced.
Identify the exact text to find and its replacement, and check whether the document uses different first-page or even-page headers and footers. Save the document as a macro-enabled Word document if you want to keep the macro in it.
Run a VBA macro across Word headers and footers
Use this macro to replace text in existing headers and footers throughout the active document.
The macro checks each section's header and footer types. It skips headers and footers linked to a previous section because their content is shared.
This example searches header and footer text ranges, not text inside shapes or text boxes. Word Find codes such as ^p have special meanings, so use ordinary text for the search and replacement unless you intend to use those codes.
Open the document in Word, then open the Visual Basic editor. On Windows, press Alt+F11.
In the VBA editor, select the document project and choose Insert > Module. Paste this code into the module: Sub ReplaceInHeadersAndFooters() Dim sec As Section Dim hf As HeaderFooter Dim rng As Range Dim findText As String Dim replaceText As String findText = "INV-001" replaceText = "INV-002" For Each sec In ActiveDocument.Sections For Each hf In sec.Headers If hf.Exists Then If sec.Index = 1 Or Not hf.LinkToPrevious Then Set rng = hf.Range With rng.Find .ClearFormatting .Replacement.ClearFormatting .Text = findText .Replacement.Text = replaceText .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With End If End If Next hf For Each hf In sec.Footers If hf.Exists Then If sec.Index = 1 Or Not hf.LinkToPrevious Then Set rng = hf.Range With rng.Find .ClearFormatting .Replacement.ClearFormatting .Text = findText .Replacement.Text = replaceText .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With End If End If Next hf Next sec End Sub
In the module, change the values assigned to findText and replaceText to the old and new text. Keep the quotation marks around each value.
Place the cursor inside ReplaceInHeadersAndFooters and press F5. Return to Word and inspect the headers and footers in each section, including first and even pages where applicable.

Update header and footer text in WPS Writer
Use WPS Writer to open a Word document and manually edit repeated header or footer text. Check each section and page variant for content that is not linked to the previous section.
- 1. Open the document: In WPS Writer, choose Open and select your Word document.
- 2. Edit a header or footer: Double-click the header or footer area on a page, select the old text, and type the replacement.
- 3. Check other sections: Navigate through the document and inspect first-page and even-page headers and footers where used. Edit any different or unlinked content that still contains the old text.
- 4. Save the changes: Choose File > Save or Save As, then review the saved document to confirm the updated text.

Frequently Asked Questions
Does the macro search every header and footer type?
It loops through each section's existing headers and footers, including primary, first-page, and even-page variants. It skips linked copies because they share content with a previous section.
Why didn't text in a header text box change?
The example searches header and footer text ranges, not text inside shapes or text boxes. Open the header or footer and edit the text box directly, or use a separate macro that searches shape text.
How do I keep the macro in my Word document?
In Word, choose File > Save As and select Word Macro-Enabled Document (*.docm). Run macros only when you trust the document and its code.
Will replacing text in one linked header affect other sections?
Yes. A header linked to a previous section shares its content, so changes to the source appear in linked sections. Open the header and check Link to Previous if you need a section to have independent content.




