Headers & Footers



Defining a separate first page header and footer for the first section

With ActiveDocument.Sections(1) 
   .PageSetup.DifferentFirstPageHeaderFooter = True
   .Footers(wdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.InsertBefore "first page footer text"
End With

Linking/Unlinking Previous

The LinkToPrevious property applies to each type of header and each type of footer separately.
Setting this to True automatically inserts text into all headers (and footers)
Setting this to False does not automatically remove the text.

objHeaderFooter.LinkToPrevious = True | False 

The following will unlink the current header from the previous header

iNoOfSections = ActiveDocument.Sections.Count 
With Application.ActiveDocument.Sections(iNoOfSections).Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary)
   .LinkToPrevious = False
   .Range.Delete ''this line deletes everything from the header to make sure it is blank
End With


The following will unlink the current footer from the previous footer

iNoOfSections = ActiveDocument.Sections.Count 
With Application.ActiveDocument.Sections(iNoOfSections).Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary)
   .LinkToPrevious = False
   .Range.Delete ''this line deletes everything from the header to make sure it is blank
End With



This creates Headers
Odd Page headers = Chapter, Page
Even Page headers = Page, Chapter

Public Sub MakeHeaders 
Dim rng As Range
Dim sect As Section
Dim sChapter As String

   For Each sect In ActiveDocument.Sections
'different odd and even page headers
      sect.PageSetup.OddAndEvenPagesHeaderFooter = True

'unlink the headers and add a tab stop at right margin
      With sect.Headers(wdHeaderFooterPrimary)
         .LinkToprevious = False
         .Range.ParagraphFormat.TabStops.ClearAll
         .Range.ParagraphFormat.TabStops.Add _
              sect.PageSetup.PageWidth - sect.PageSetup.RightMargin - sect.PageSetup.LeftMargin, _
              wdTabAlignment.wdAlignTabRight
      End With

'repeat for even page header
      With sect.Headers(wdHeaderFooterEven)
         .LinkToprevious = False
         .Range.ParagraphFormat.TabStops.ClearAll
         .Range.ParagraphFormat.TabStops.Add _
              sect.PageSetup.PageWidth - sect.PageSetup.RightMargin - sect.PageSetup.LeftMargin, _
              wdTabAlignment.wdAlignTabRight
      End With

'get chapter X text from first paragraph in section
      sChapter = sect.Range.Paragraphs(1).Range.Text

'trim paragraph mark if present
      If Right(sChapter, 1) = vbCr Then
         sChapter = Left(sChapter, Len(sChapter, 1)
      End If

'do odd-page (primary header)
      Set rng = sect.Headers(wdHeaderFooterPrimary).Range
      rng.Text = sChapter & vbTab & "page "
      rng.Collapse wdCollapseEnd
'insert page number
      rng.Fields.Add rng, wdFieldType.wdFieldPage
'insert chapter number
      Set rng = sect.Headers(wdHeaderFooterEvenPages).Range
      rng.Collapse wdCollapseEnd
      rng.InsertAfter vbTab & sChapter

   Next sect
End Sub



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrev