Building custom solutions that extend, automate, and integrate Microsoft 365 apps.
Here's a precise way to remove all headers and footers (including <w:headerReference> and <w:footerReference> tags) without deleting section breaks in Microsoft Word 2019.
Solution: Use Word’s "Open XML" View to Remove Header/Footer References
Since you want to preserve section breaks and remove only the header/footer references from the document XML, follow these steps:
Option 1: Use a Macro to Clean All Header/Footer References
You can run a VBA macro that clears all headers and footers from each section, and ensures the XML references are removed too.
VBA Macro:
- Press
Alt + F11to open the VBA editor.
Insert a new module: Insert > Module.
Paste the code below:
Sub RemoveHeadersAndFootersKeepSections()
Dim sec As Section
For Each sec In ActiveDocument.Sections
With sec.Headers(wdHeaderFooterPrimary)
.Range.Delete
End With
With sec.Footers(wdHeaderFooterPrimary)
.Range.Delete
End With
With sec.Headers(wdHeaderFooterFirstPage)
.Range.Delete
End With
With sec.Footers(wdHeaderFooterFirstPage)
.Range.Delete
End With
With sec.Headers(wdHeaderFooterEvenPages)
.Range.Delete
End With
With sec.Footers(wdHeaderFooterEvenPages)
.Range.Delete
End With
Next sec
End Sub
Run the macro: Press F5 or Run from the toolbar.
This clears all types of headers and footers for every section while keeping section breaks intact. It also removes the header/footer references in the Open XML structure.