Headers & Footers


HeaderFooter object

A HeaderFooter object represents either a single header or footer.
The headerfooter object can represent either a header or footer. This object is available from the Selection obect
A HeaderFooters collection contain HeaderFooter objects of one type (header or footer).
The HeadersFooters collection includes all headers and footers in the specified document section.


A HeaderFooter collection, whether it be a collection of headers or a collection of footers is indexed by one of the constants in the following enumeration.


wdInformation.wdInHeaderFooter 
wdInformation.wdHeaderFooterType

There are three different types of headers and three types of footers
Primary
First Page
Even Pages


Primary

To obtain the HeaderFooter object that represents the primary header in the first section of the active document

Dim objHeaderFooter As HeaderFooter 
objHeaderFooter = ActiveDocument.Sections(1).Headers wdHeaderFooterIndex.wdHeaderFooterPrimary

objHeaderFooter = ActiveDocument.Sections(1).Footers wdHeaderFooterIndex.wdHeaderFooterPrimary 

The following code changes the text in both the primary header and the primary footer

With ActiveDocument.Sections(1) 
   .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = "some text"
   .Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = "some text"
End With


Current Selection

Note that if the current selection is within a header or footer then the HeaderFooter property of the Selection object will return the active HeaderFooter object.

objHeaderFooter = Selection.HeaderFooter 


Activating

ActiveWindow.ActivePane.View.SeekView = wdSeekView.wdSeekCurrentPageHeader 
ActiveWindow.ActivePane.View.SeekView = wdSeekView.wdSeekCurrentPageFooter


Deactivating

ActiveWindow.ActivePane.View.SeekView = wdSeekView.wdSeekMainDocument 


Modifying ??

With ActiveDocument.Sections(1).Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary) 
   .Delete
   .Fields.Add range := Selection, type := wdfieldname, text := "\p"
   .InsertAfter text := vbTab
   .InsertAfter text := vbTab
   .Collapse direction := wdCollapseDirection.wdCollapseStart
   .Fields.Add range := Selection, type := fieldauthor
End With

Does a Header or Footer Exists

In any document the three types of headerfooter objects always exist although the property will be set to False when they are not visible or not being used within a document.


The Exists property can be used to determine whether a first page or odd page header / footer exists

Dim bExists As Boolean 
bExists = ActiveDocument.Sections(1).Headers(wdHeaderFooterIndex.wdHeaderFooterFirstPage).Exists


bExists = ActiveDocument.Sections(1).Headers(wdHeaderFooterIndex.wdHeaderFooterEvenPages).IsHeader 
bExists = ActiveDocument.Sections(1).Footers(wdHeaderFooterIndex.wdHeaderFooterEvenPages).IsFooter

Different First Page

Use the DifferentFirstPageHeaderFooter property with the PageSetup object to specify a different first page.
The following example inserts text into the first page footer in the active document.

With ActiveDocument 
    .PageSetup.DifferentFirstPageHeaderFooter = True
    .Sections(1).Footers(wdHeaderFooterFirstPage) _
        .Range.InsertBefore _
        "Written by Joe Smith"
End With


objPageSetup.DifferentFirstPagePagesHeaderFooter = True / False 


Different Odd Even

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



Insert an image into a header


Sub AddImageToHeader() 
Dim SrcePath As String

SrcePath = "C:\Users\Chris\Desktop\Testing\Test.png"

ThisDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary) _
  .Range.InlineShapes.AddPicture (SrcePath)

ThisDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
  .Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
End Sub

Insert a table into a header


Sub UpdateHeader() 
    Dim oDoc As Word.Document, oSec As Word.Section, rng As Word.Range
    Set oDoc = ActiveDocument

    For Each oSec In oDoc.Sections
        Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
        AddHeaderToRange rng

        Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        AddHeaderToRange rng
    Next oSec
End Sub

Private Sub AddHeaderToRange(rng As Word.Range)
    With rng
        .Tables.Add Range:=rng, NumRows:=1, NumColumns:=2
        With .Tables(1)
            .Borders.InsideLineStyle = wdLineStyleNone
            .Borders.OutsideLineStyle = wdLineStyleNone
            .Rows.SetLeftIndent LeftIndent:=-37, RulerStyle:=wdAdjustNone
            .Columns(2).SetWidth ColumnWidth:=300, RulerStyle:=wdAdjustNone
            .Cell(1, 1).Range.InlineShapes.AddPicture FileName:="Your Pic Solution", LinkToFile:=False, SaveWithDocument:=True
            .Cell(1, 2).Range.Font.Name = "Arial"
            .Cell(1, 2).Range.Font.Size = 9
            .Cell(1, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
            .Cell(1, 2).Range.Text = "Test header" & vbNewLine & "Second Line"
        End With
    End With
End Sub

Sub UpdateHeaders() 
Dim Tbl As Table, Sctn As Section
With ActiveDocument
  Set Tbl = .Tables.Add(Range:=.Range(0, 0), NumRows:=1, NumColumns:=2)
  With Tbl
    .Borders.InsideLineStyle = wdLineStyleNone
    .Borders.OutsideLineStyle = wdLineStyleNone
    .Rows.SetLeftIndent LeftIndent:=-37, RulerStyle:=wdAdjustNone
    .Columns(2).SetWidth ColumnWidth:=300, RulerStyle:=wdAdjustNone
    .Cell(1, 1).Range.InlineShapes.AddPicture FileName:="Your Pic Solution", LinkToFile:=False, SaveWithDocument:=True
    .Cell(1, 2).Range.Font.Name = "Arial"
    .Cell(1, 2).Range.Font.Size = 9
    .Cell(1, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
    .Cell(1, 2).Range.Text = "Test header" & vbCr & "Second Line"
  End With
  For Each Sctn In .Sections
    With Sctn
      With .Headers(wdHeaderFooterPrimary)
        If .LinkToPrevious = False Then .Range.FormattedText = Tbl.Range.FormattedText
      End With
      With .Headers(wdHeaderFooterFirstPage)
        If .LinkToPrevious = False Then .Range.FormattedText = Tbl.Range.FormattedText
      End With
    End With
  Next
  Tbl.Delete
End With
End Sub


Insert page numbers into a footer


Sub testing() 

Selection.TypeText "Page "
Selection.Fields.Add Range:=Selection.Range, _
                     Type:=WdFieldType.wdFieldPage

Selection.TypeText " out of "
Selection.Fields.Add Range:=Selection.Range, _
                     Type:=WdFieldType.wdFieldNumPages


Selection.TypeText "first"
Selection.TypeText vbTab
Selection.TypeText "second"

Dim oRange As Word.Range

    Set oRange = ActiveDocument.Bookmarks("BM_FooterSection2").Range
    oRange.Text = "monday"
    ActiveDocument.Bookmarks.Add "BM_FooterSection2", oRange

    Set oRange = ActiveDocument.Bookmarks("BM_FooterSectionPgNo").Range
    oRange.Text = "tuesday"
    ActiveDocument.Bookmarks.Add "BM_FooterSectionPgNo", oRange
    
End Sub


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