Page Numbers

The PageNumbers property of the HeaderFooter object returns a PageNumbers collection, which holds all of the PageNumber objects for that particular header or footer.
In most cases a header (or footer) will contain a single pagenumber object (if any) denoted by PageNumbers(1).
To add a page number to a header or footer
The Add method inserts a text frame containing the page number field into the specified header or footer
The text frame is positioned according to the PageNumberAlignment parameter.
If FirstPage is set to False a page number is not added to the first page

objPageNumbers.Add PageNumberAlignment:=wdPageNumberAlignment.wdAlignPageNumberLeft, 
                   FirstPage:= True | False

If you want to insert a page number within some other text then you can use the Fields.Add method using the type wdFieldPage

Dim objRange As Range 
   Set objRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range
   objRange.ParagraphFormat.Alignment = wdParagraphAlignment.wdAlignParagraphCenter
   objRange.Text = "some text"
   objRange.Collapse wdCollapseDirection.wdCollapseEnd
   objRange.Fields.Add objRange, wdFieldPage
   objRange.Move wdStory
   objRange.InsertAfter "some more text"


Inserting Page Numbers


activedocument.footnotes.add range:=myrange, text:="my text" 
With Selection.Sections(1).Headers(wdHeaderFooterPrimary).Pagenumbers
   .NumberStyle = wdPageNumberStyle.wdPageNumberStyleArabic
   .HeadingLevelForChapter = 0
   .IncludeChapterNumber = False
   .ChapterPageSeperator = wdSeparatorType.wdSeperatorHyphen
   .RestartNumberingAtSection = True
   .StartNumbering = 5
End With


ipageno = Application.Selection.Information(WdInformation.wdActiveEndPageNumber) 

oFooter.PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter, _ 
                        FirstPage:=True


Return the Page Number at the current cursor position

Selection.Information(wdActiveEndPageNumber) 
ActiveDocument.Content.Information(wdInformation.wdActiveEndPageNumber)

Return the number of pages in the active document

Selection.Information(wdInformation.wdNumberOfPagesInDocument) 

ActiveDocument.BuiltInDocumentProperties("Number of Pages") 

The last method is the most reliable, but also the slowest

ActiveDocument.Content.ComputeStatistics(wdStatistic.wdStatisticsPages) 



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext