Word: Linking and unlinking all headers and footers
Link all headers and footers to each other
After you've got the first header and footer fixed up to your liking, this macro will then link them all so that this header and footer is copied across all sections.
Public Sub LinkHeadersAndFooters() ' Links all headers and footers apart from the first one.
Count = 0
For Each mySection In ActiveDocument.Sections
Count = Count + 1
If Count > 2 Then 'Iterate over each sections (apart from the first) and unlink headers and footers...
With ActiveDocument.Sections(mySection.Index) 'Link header...
With .Headers(wdHeaderFooterPrimary)
.LinkToPrevious = True
End With 'Link footer...
With .Footers(wdHeaderFooterPrimary)
.LinkToPrevious = True
End With
End With
End If
Next mySection
End Sub
Unlink all headers and footers from each other
After you link all the headers and footers to copy the data, sometimes you need to unlink the headers and footers different to each other, especially if they are portrait and landscape. Here's a way to unlink them all with a macro.
Sub UnlinkHeadersAndFooters()
Dim sec As Section
Dim hdr As HeaderFooter
Dim ftr As HeaderFooter
' Loop through each section in the document
For Each sec In ActiveDocument.Sections
' Deselect "Different First Page"
sec.PageSetup.DifferentFirstPageHeaderFooter = False
' Loop through each header in the section
For Each hdr In sec.Headers
hdr.LinkToPrevious = False
Next hdr
' Loop through each footer in the section
For Each ftr In sec.Footers
ftr.LinkToPrevious = False
Next ftr
' Check if the section break is not a new page. This works around a Word bug,
' where Continuous breaks or column breaks have an invisible header and footer.
' If it is not a new page break, then set this section to LinkToPrevious so that
' it properly inherits the headers and footers ready for the next section.
If sec.PageSetup.SectionStart <> wdSectionNewPage Then
' Link the header to the previous section
sec.Headers(wdHeaderFooterPrimary).LinkToPrevious = True
sec.Footers(wdHeaderFooterPrimary).LinkToPrevious = True
End If
Next sec
End Sub