Personal website of Gregory K. Maxey, Commander USN (Retired)
Do you have ad-blocking software enabled? While I respect your right to do so, your donations and the minimal advertisements on this site help to defray internet and other costs of providing this content. Please consider excluding this website from blocking or turning off the blocker while browsing this site.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
The purpose of this Microsoft Word Tips & Microsoft Word Help page is provide a VBA solution to automatic linked headers and footers.
Sections in Word can be fickle (see InfoBit Blog Section Breaks). One thing that I don't like about the built-in user interface for adding sections is the header and footer of any new section inserted is linked to the previous section header and footer by default and "stays that way" without further user actions. As shown below, the header text in section two is linked by default to the header text in section one. While this feature is very handy for repeating header and footer information in subsequent sections, I think it becomes a nuisance when you want to change part or all of the header/footer text in your new sections.
In the illustration below I've started with a single section document containing header text and then added a section break. When adding the new section the header text in section one is automatically applied to the header text in section two.
Let's change the generic section two header text to:
Easy enough, but to our chagrin and the chagrin of countless other Word users, the overall effects can seem disastrous. My point is illustrated if you go back and see the effect that the change in section two header had on the section one header:
The cause of this folly is that headers and footers are linked to the previous header and footer when a new section is added via the user interface.
To further complicate matters, a Word document with sections is sort of like an old fashioned player piano sheet. While the section one header does not display "Same as Previous," the last section of a document (in our case section two) functions in effect as the previous section to section one. That is why when sections are linked, changes in subsequent sections can confusingly roll through a document changing other section's header and footer text.
Using a VBA procedure in lieu of a the user interface, you can create a section break and carry over the previous section text. However, once the section and carry over text is created the link is broken. This allows you to make changes in the new header or footer that won't affect the header and footer of the previous section.
Option Explicit Dim i As Long Dim j As Long Dim oDoc As Word.Document Sub AddSectionAndKillLinkToPrevious() Dim myRng As Word.Range Set oDoc = ActiveDocument Selection.InsertBreak Type:=wdSectionBreakNextPage 'Get the index number of the added section i = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count With oDoc.Sections(i) For j = 1 To 3 .Headers(j).LinkToPrevious = False .Footers(j).LinkToPrevious = False Next j End With 'Note: j provides the constant value to unlink all three header\footer types. lbl_Exit: Exit Sub End Sub Sub RelinkSections() Dim myRng As Word.Range Set oDoc = ActiveDocument For i = 1 To oDoc.Sections.Count With oDoc.Sections(i) For j = 1 To 3 .Headers(j).LinkToPrevious = True .Footers(j).LinkToPrevious = True Next j End With Next i lbl_Exit: Exit Sub End Sub
1. The procedure RelinkSections is provided in case you should want to quickly relink all sections in your document.
2. See: Installing Macros for instructions on how to set up and use the macros provided in this tips page.
That's it! I hope you have found this tips page useful and informative.
Do you want to make a payment for consulting work or donate to help support this site?
PayPal is a safe, easy way to pay online.
Use the appropriate currency "Donate" button to make a payment or donation.