Sunday, October 29, 2023

Word Macro Insert Page X of Y

Sub InsertTotalPages()
' Inserts total number of pages in the document at cursor
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "NUMPAGES  ", PreserveFormatting:=True
End Sub
 

Sub InsertPageXofY()
' Inserts Page {current_page} of {total_doc_pages} at cursor
    Selection.TypeText Text:="Page "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage, Text:="Page ", _
        PreserveFormatting:=True
    Selection.TypeText Text:=" of "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "NUMPAGES  ", PreserveFormatting:=True
End Sub