Different Odd Even
Headers and Footers
Use the OddAndEvenPagesHeaderFooter property with the PageSetup object to specify different odd and even page headers and footers.
If the OddAndEvenPagesHeaderFooter property is True, you can return an odd header or footer by using wdHeaderFooterPrimary, and you can return an even header or footer by using wdHeaderFooterEvenPages.
Use the Add method with the PageNumbers object to add a page number to a header or footer.
The following example adds page numbers to the primary footer in the first section of the active document.
With ActiveDocument.Sections(1)
.Footers(wdHeaderFooterPrimary).PageNumbers.Add
End With
objPageSetup.OddAndEevenPagePagesHeaderFooter = True / False
The OddAndEvenPagesHeaderFooter property of the PageSetup object can be used to specify different odd and even page headers and footers.
When this property is True, the index wdHeaderFooterPrimary refers to the odd header or footer.
The primary header (or footer) is defined based on the values of two properties:
The primary header is:
1) The only header when DifferentFirstPageHeaderFooter = False and OddAndEvenPagesHeaderFooter = False
2) The non first page header when DifferentFirstPageHeaderFooter = True and OddAndEvenPagesHeaderFooter = False
3) The odd page header when DifferentFirstPageHeaderFooter = False and OddAndEvenPagesHeaderFooter = True
4) The non first page odd header when DifferentFirstPageHeaderFooter = True and OddAndEvenPagesHeaderFooter = True
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 TopPrevNext