I'm trying to write a macro using VBA in Microsoft Word that will do the following:
Take an input of a starting number and an ending number.
Insert the number of pages that is the difference between the starting number and ending number minus one (to account for the original page) into the document.
Add the respective numbers to the footer of each page.
So, if the inputted starting number is 5 and the ending number is 10, a total of 5 new pages should be inserted into the document. Then, the first page's footer should say "Page 5", the second "Page 6", etc. etc. all the way to "Page 10".
This code works well to insert the pages, but beyond that, I can't get the numbers to properly insert.
Sub InsertPages()
Dim startPage As Integer
Dim endPage As Integer
Dim i As Integer
' Prompt the user to input the starting and ending page numbers
startPage = InputBox("Enter the starting page number:", "Starting Page")
endPage = InputBox("Enter the ending page number:", "Ending Page")
' Validate input
If startPage >= endPage Or startPage < 1 Or endPage < 1 Then
MsgBox "Invalid page numbers. Please enter valid page numbers.", vbExclamation
Exit Sub
End If
' Loop through and insert pages
For i = startPage To endPage - 1
Selection.InsertBreak Type:=wdPageBreak
Next i
End Sub
Option 1: section break + static page number as text
To get each page has its own footer, two conditions must be met:
Microsoft documentation:
Option Explicit
Sub InsertPageFooter()
Dim oSec As Section, oRng As Range, i As Long
Dim startPage As Long
Dim endPage As Long
' Prompt the user to input the starting and ending page numbers
startPage = InputBox("Enter the starting page number:", "Starting Page")
endPage = InputBox("Enter the ending page number:", "Ending Page")
' Use initial values for testing
' startPage = 5: endPage = 10 ' for testing
' Validate input
If startPage >= endPage Or startPage < 1 Or endPage < 1 Then
MsgBox "Invalid page numbers. Please enter valid page numbers.", vbExclamation
Exit Sub
End If
' Insert Section Break
For i = startPage To endPage - 1
Selection.InsertBreak Type:=wdSectionBreakNextPage
Next
i = 0
' Update footer
For Each oSec In ActiveDocument.Sections
oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
Set oRng = oSec.Footers(wdHeaderFooterPrimary).Range
oRng.Text = "Page " & startPage + i
i = i + 1
Next
End Sub
Option 2: page break + PAGE field
Option Explicit
Sub InsertPageFooter2()
Dim oSec As Section, oRng As Range, i As Long
Dim startPage As Long
Dim endPage As Long
' Prompt the user to input the starting and ending page numbers
startPage = InputBox("Enter the starting page number:", "Starting Page")
endPage = InputBox("Enter the ending page number:", "Ending Page")
' Use initial values for testing
' startPage = 5: endPage = 10 ' for testing
' Validate input
If startPage >= endPage Or startPage < 1 Or endPage < 1 Then
MsgBox "Invalid page numbers. Please enter valid page numbers.", vbExclamation
Exit Sub
End If
' Insert Page Break
For i = startPage To endPage - 1
Selection.InsertBreak Type:=wdPageBreak
Next
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
Set oRng = .Range
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:="PAGE \* Arabic ", PreserveFormatting:=True
oRng.InsertBefore "Page "
oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
With .PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = startPage
End With
End With
End Sub