Get the number of pages in a Word document Get the number of pages in a Word document vba vba

Get the number of pages in a Word document


I just spent a good 2 hours trying to solve this, and I have yet to see this answer on any forum so I thought I would share it.

https://msdn.microsoft.com/en-us/vba/word-vba/articles/pages-object-word?f=255&MSPPError=-2147217396

That gave me my solution combined with combing through the articles to find that most of the solutions people reference are not supported in the newest versions of Word. I don't know what version it changed in, but my assumption is that 2013 and newer can use this code to count pages:

ActiveDocument.ActiveWindow.Panes(1).Pages.Count.

I believe the way this works is ActiveDocument selects the file, ActiveWindow confirms that the file to be used is in the current window (in case the file is open in multiple windows from the view tab), Panes determines that if there is multiple windows/split panes/any other nonsense you want the "first" one to be evaluated, pages.count designates the pages object to be evaluated by counting the number of items in the collection.

Anyone more knowledgeable feel free to correct me, but this is the first method that gave me the correct page count on any document I tried!

Also I apologize but I cant figure out how to format that line into a code block. If the mods want to edit my comment to do that be my guest.


Try (maybe after ActiveDocument.Repaginate)

ActiveDocument.BuiltinDocumentProperties(wdPropertyPages)

It is causing my Word 2010 to spend half-second with "Counting words" status in status bar, while ActiveDocument.ComputeStatistics(wdStatisticPages) returns the result immediately.

Source: https://support.microsoft.com/en-us/kb/185509


After you've made all your changes, you can use OnTime to force a slight delay before reading the page statistics.

Application.OnTime When:=Now + TimeValue("00:00:02"), _        Name:="UpdateStats"

I would also update all the fields before this OnTime statement:

ActiveDocument.Range.Fields.Update