The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
This Microsoft Word Tips & Microsoft Word Help page provides some helpful links to page numbering reference material plus some tips of my own.
The Word MVP FAQ site has several helpful articles on page numbering: Numbering FAQs
Sometimes people ask: How to display both "Page X of Y Pages" and "Page X of Z Pages" in the header/footer of a document?
Note: I use the term "content section" to differentiate and define a document section formed with section breaks consisting of like content (e.g., Marketing, Sales, etc.) from sections formed with section breaks for the purpose of document layout (e.g., page orientation, etc.)
For this demonstration I have used an eight page document containing three sections. Section 1 contains two pages, section 2 contains three pages and section 3 contains three pages. The illustration below depicts dual numbering in section 1:
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.
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
What if the document contains one or more single "content" sections that requires two or more sections to facilitate page layout or formatting? There's a twist!
In this situation, using the method described earlier, your page numbering is this section is all "twisted" up!
Again, to resolve this issue you can deduce the and display the correct number of "content" section pages using bookmarks, PageRef fields a forumla field.
Note: Special thanks to friend and Word MVP Doug Robbins for his feedback, and suggestions for improving the dual numbering methods outlined above.
I have a cover page on my document that I don't want numbered (or "How do I start numbering with Page 1 on the second or other subsequent page of my document?)
Note: Using the conditional IF method works for simple page numbering. It will introduce errors if you attempt to use a Table of Contents in your document.
If you want to print on the front and back of your paper but only number the front side with a consecutive number, use a "different odd and even" header/footer layout and put the following field construction in the odd page headers:
If you want to to number each page in odd or even increments you can use:
That's it! I hope you have found this tips page useful and informative.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!