View Single Post
 
Old 08-06-2019, 02:23 PM
Swarup Swarup is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 185
Swarup is on a distinguished road
Default

Another option is to use the VBA Script that Greg provided. Here it is below.

Here is Greg's description for its utility:

Quote:
You can use the following VBA procedure to quickly add the necessary bookmarks and nested fields codes needed for section numbering in a document. The code allows you to add the section page numbering centered in either the headers or footers of your document. From there you can easily cut and paste them to better suit your requirements.
Code:
Sub InsertSectionPageNumbering()
'Developed by Greg Maxey and Doug Robbins.
Dim lngIndex As Long, lngHF As Long
Dim oRng As Range
Dim oFld As Field, oFldQ As Field
Dim strPlacement As String
Dim bUserSetting As Boolean
  Select Case UCase(InputBox("Enter ""F"" to place section numbering in footers." _
         & "Enter ""H"" to place section numbering in headers.", "Placement", "F"))
    Case "H"
      strPlacement = "Headers"
    Case "F"
      strPlacement = "Footers"
    Case Else
      Exit Sub
  End Select
  With ActiveDocument
    For lngIndex = 1 To .Sections.Count
      .Bookmarks.Add "S" & lngIndex, .Sections(lngIndex).Range.Characters.Last
    Next lngIndex
    bUserSetting = ActiveWindow.View.ShowFieldCodes
    ActiveWindow.View.ShowFieldCodes = True
    FirstSectionSetup .Sections(1), strPlacement
    For lngIndex = 2 To .Sections.Count
      AllOtherSectionSetup .Sections(lngIndex), lngIndex, strPlacement
    Next lngIndex
   .Fields.Update
    ActiveWindow.View.ShowFieldCodes = bUserSetting
  End With
End Sub

Sub FirstSectionSetup(ByRef oSect As Section, Optional strPlacement As String = "Footers")
Dim lngIndex As Long, lngHF As Long
Dim oRng As Range
Dim oFld As Field, oFldQ As Field
Dim oHDs As HeadersFooters

  With oSect
    If strPlacement = "Footers" Then
      Set oHDs = .Footers
    Else
      Set oHDs = .Headers
    End If
    For lngHF = 1 To oHDs.Count
      With oHDs(lngHF)
        Set oRng = .Range
        For Each oFld In oRng.Fields
          If InStr(oFld.Code, " QUOTE") > 0 Then
            oFld.Delete
            Exit For
          End If
        Next
        Set oFldQ = ActiveDocument.Fields.Add(oRng, wdFieldQuote, _
                    PreserveFormatting:=False)
        Set oRng = oFldQ.Code '.Characters.Last.Previous '.Previous '.Previous
        oRng.Collapse wdCollapseEnd
        oRng.Text = """Page "
        oRng.Collapse wdCollapseEnd
        ActiveDocument.Fields.Add oRng, wdFieldPage, PreserveFormatting:=False
        Set oRng = oFldQ.Code.Characters.Last '.Previous
        oRng.Collapse wdCollapseEnd
        oRng.Text = " of "
        Set oRng = oFldQ.Code.Characters.Last
        oRng.Collapse wdCollapseEnd
        ActiveDocument.Fields.Add oRng, wdFieldSectionPages, PreserveFormatting:=False
        Set oRng = oFldQ.Code.Characters.Last
        oRng.Collapse wdCollapseEnd
        oRng.Text = " Section Pages"""
        .Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
      End With
    Next lngHF
  End With
End Sub

Sub AllOtherSectionSetup(ByRef oSect As Section, ByRef lngIndex As Long, _
                         Optional strPlacement As String = "Footers")
Dim lngHF As Long
Dim oRng As Range
Dim oFld As Field, oFldQ As Field
Dim oHDs As HeadersFooters
  With oSect
    If strPlacement = "Footers" Then
      Set oHDs = .Footers
    Else
      Set oHDs = .Headers
    End If
    For lngHF = 1 To oHDs.Count '.Footers.Count
      With oHDs(lngHF) '.Footers(lngHF)
        Set oRng = .Range
        .LinkToPrevious = False
        For Each oFld In oRng.Fields
          If InStr(oFld.Code, " QUOTE") > 0 Then
            oFld.Delete
            Exit For
          End If
        Next
        Set oRng = .Range
        oRng Collapse wdCollapseStart
        Set oFldQ = ActiveDocument.Fields.Add(oRng, wdFieldQuote, _
                    PreserveFormatting:=False)
        Set oRng = oFldQ.Code
        oRng.Collapse wdCollapseEnd
        oRng.Text = """Page "
        oRng.Collapse wdCollapseEnd
        Set oFld = ActiveDocument.Fields.Add(oRng, wdFieldEmpty, "= ", _
                   PreserveFormatting:=False)
        Set oRng = oFld.Code.Characters.Last.Previous
        ActiveDocument.Fields.Add oRng, wdFieldPage, PreserveFormatting:=False
        Set oRng = oFld.Code.Characters.Last
        oRng.Collapse wdCollapseEnd
        oRng.Text = "- "
        Set oRng = oFld.Code.Characters.Last
        ActiveDocument.Fields.Add oRng, wdFieldEmpty, _
                 Text:="PAGEREF S" & lngIndex - 1, PreserveFormatting:=False
        Set oRng = oFldQ.Code.Characters.Last '.Previous
        oRng.Collapse wdCollapseEnd
        oRng.Text = " of "
        Set oRng = oFldQ.Code.Characters.Last
        oRng.Collapse wdCollapseEnd
        ActiveDocument.Fields.Add oRng, wdFieldSectionPages, PreserveFormatting:=False
        Set oRng = oFldQ.Code.Characters.Last
        oRng.Collapse wdCollapseEnd
        oRng.Text = " Section Pages"""
        .Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
      End With
    Next lngHF
  End With
End Sub
The questions I have for this are:
1. Greg says above that this script will place the needed X of Z format in the middle of the header or footer. Which is the script going to put it in-- the header or the footer?

I already have page numbering for this 300 page document set up in the middle of the footer, as well as in the outside corner of the header. Is this script going to disturb that arrangement? I had put a lot of work into setting that up the way I wanted it.

2. Is this VBA script ready to go? And is it going to bring about what I had described in my first post on this thread, i.e. an X of Z (section pages) that I can then cut and paste into the right lower corner of my footer?

I am also ready to set this up myself manually for each chapter. Just need a bit of guidance to solve the syntax error described in the previous post.
Reply With Quote