0
votes

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:

  1. Restart page number from Section 5

  2. 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 #"

2
What are you asking, here? Are you asking for all the field code logic, as the question seems to imply? Or just for the commented out part of the code? If the latter, see stackoverflow.com/q/49804968/3077495. But you might want to consider storing the entire field code as a Building Block in the template, then simply insert the building block as required.Cindy Meister
Hi Cindy - just to clarify, I'm asking for what additional code I need to put in my code above to replace the bit that I've commented as: 'NEED CODE HERE TO INSERT THE FOLLOWING FIELD LOGIC INTO FOOTER' I've looked at the article you linked to, and although it looks like it could provide a solution, I had some difficulty figuring out where I would start to use it to be able to insert my field logic i.e. { if { page } < { = { pageref ReferencesEnd } + 1 } "Page { = { page } } of { = { pageref ReferencesEnd }" "{Styleref "Att-Appendix Heading" \n } Any tips on where to start?JayNG

2 Answers

0
votes

Although it is possible to create complex field structures via VBA, you'd do better to store the required field codes in two separate paragraphs in a source document from where your macro can copy & paste them into the appropriate locations in the target documents. With that approach you might use code like:

Sub Demo()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document
Dim i As Long, Rng As Range, HdFt As HeaderFooter
Set DocSrc = ThisDocument
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .Title = "Select the target file"
  .AllowMultiSelect = False
  If .Show = -1 Then
    Set DocTgt = Documents.Open(.SelectedItems(1))
  Else
    MsgBox "No target file selected. Exiting", vbExclamation
    GoTo ErrExit
  End If
End With
With DocTgt
  For i = 1 To .Sections.Count
    Select Case i
      Case 1 To 4: Set Rng = DocSrc.Paragraphs(1).Range
      Case Else: Set Rng = DocSrc.Paragraphs(2).Range
    End Select
    With .Sections(i)
      For Each HdFt In .Footers
        With HdFt
          If .Exists Then
            If .LinkToPrevious = False Then
              .Range.FormattedText = Rng.FormattedText
              .Range.Characters.Last.Delete
            End If
          End If
        End With
      Next
    End With
  Next
End With
ErrExit:
Set Rng = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
End Sub

If necessary, though it seems unlikely, you can supplement the above with code to apply the desired numbering format - or you can add the appropriate switches to the field codes themselves.

PS: Your second field code could be reduced to-

{IF{PAGE}< {={PAGEREF ReferencesEnd}+1} "Page {PAGE} of {PAGEREF ReferencesEnd}" {STYLEREF "Att-Appendix Heading" \n}}
0
votes

As previously indicated it is possible to create complex field structures via VBA. For that approach you might use code like:

Sub Demo()
Application.ScreenUpdating = False
Dim DocTgt As Document, StrCode As String
Dim i As Long, Rng As Range, HdFt As HeaderFooter
With ActiveDocument
  For i = 1 To .Sections.Count
    Select Case i
      Case 1 To 4
        With .Sections(i)
          For Each HdFt In .Footers
            With HdFt
              If .Exists Then
                With .PageNumbers
                  .NumberStyle = wdPageNumberStyleLowercaseRoman
                  .RestartNumberingAtSection = False
                End With
                If .LinkToPrevious = False Or i = 1 Then
                  .Range.Fields.Add .Range, wdFieldEmpty, "PAGE", False
                End If
              End If
            End With
          Next
        End With
      Case Else:
        With .Sections(i)
          For Each HdFt In .Footers
            With HdFt
              If .Exists Then
                If i = 5 Then
                  .LinkToPrevious = False
                  With .PageNumbers
                    .NumberStyle = wdPageNumberStyleArabic
                    .RestartNumberingAtSection = True
                    .StartingNumber = 1
                  End With
                Else
                  With .PageNumbers
                    .NumberStyle = wdPageNumberStyleArabic
                    .RestartNumberingAtSection = False
                  End With
                End If
                If .LinkToPrevious = False Then
                  With .Range
                    .Fields.Add .Duplicate, wdFieldEmpty, "IF<  ""Page  of """, False
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 19
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "STYLEREF ""Att-Appendix Heading"" \n", False
                    End With
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 17
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "PAGEREF ReferencesEnd", False
                    End With
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 13
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "PAGE", False
                    End With
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 6
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "=+1", False
                      .Start = .Start + 3
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "PAGEREF ReferencesEnd", False
                    End With
                    .Start = .Start + 4
                    .Collapse wdCollapseStart
                    .Fields.Add .Duplicate, wdFieldEmpty, "PAGE", False
                  End With
                End If
              End If
            End With
          Next
        End With
    End Select
  Next
End With
End Sub