I have a word template which uses different footer fields depending on the section. Occasionally, users of this template will mess up the footers, and so I'm writing a macro to fix the footers by putting the default footer fields back in.
The footer fields have some field logic in them based on the section, and basically I need to do the following:
Restart page number from Section 5
Insert text into a table in the footer in row 1, column 2 based on the sections as per below
Sections 1 to 4: { PAGE } //Note that this is in Roman numeral format, with 'Different first page' option set for the footer
Sections 5 onward { if { page } < { = { pageref ReferencesEnd } + 1 } "Page { = { page } } of { = { pageref ReferencesEnd }" "{Styleref "Att-Appendix Heading" \n }"
I've managed to get the first step done and the field inserted for sections 1 to 4, however I'm struggling with How can I programmatically insert the complex field logic for Section 5+ into the relevant footers in my template using VBA? The code I need is commented in the code block below as: 'NEED CODE HERE TO INSERT THE FOLLOWING FIELD LOGIC INTO FOOTER
Sub FixPageNumbering()
Dim intSect As Integer
On Error Resume Next
'Insert footer code for Sections 1-4 into row1,col1 of 2x2 table
For intSect = 1 To 4
With ActiveDocument.Sections(intSect).Footers(wdHeaderFooterPrimary)
.PageNumbers.NumberStyle = wdPageNumberStyleLowercaseRoman
.Range.Tables(1).Rows(1).Cells(2).Select
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
End With
Next intSect
'Set page numbering to restart at #1 from Section 5
With ActiveDocument.Sections(5).Footers(wdHeaderFooterPrimary).PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
'Insert footer code for Sections 5 and onwards into row1,col1 of 2x2 table
For intSect = 5 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(intSect).Footers(wdHeaderFooterPrimary)
.PageNumbers.NumberStyle = wdPageNumberStyleArabic
.Range.Tables(1).Rows(1).Cells(2).Select
'NEED CODE HERE TO INSERT THE FOLLOWING FIELD LOGIC INTO FOOTER
'{ if { page } < { = { pageref ReferencesEnd } + 1 } "Page { = { page } } of { = { pageref ReferencesEnd }" "{Styleref "Att-Appendix Heading" \n }"
End With
Next intSect
ActiveWindow.View.Type = wdPrintView
End Sub
For sections 5 and onward, the footer field should either display Page # of &, or when there is an Appendix (for pages existing after a ReferencesEnd bookmark) it will display "Appendix #"