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



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