I have various word documents that have page numbers in header but also a field with just the number of pages somewhere on page 1.
Header page number format is: PAGE / PAGES with the appropriate field codes.
The number on page 1 is just: PAGES with the appropriate field code { NUMPAGES \* MERGEFORMAT}
While header page numbers auto update when inserting a new page or removing one, the number of pages on page 1 doesn’t update.
I know there are keyboard shortcuts for updating, I know I can make a simple vba code linked to a button in the Quick Access Toolbar.
But the end user knows none of this and is interested in the number of pages to auto update so she doesn’t have to do additional steps. Because she says if additional steps are required, what’s the point in having the field there that doesn’t auto update? She could write the number of pages manually.
So I’ve been trying to update the field via the AutoOpen sub. It’s not working well. I’ve tried various codes for updating all fields in a document. They all work when using a button in the QAT, but none of them works well on document open. I’ve tried both AutoOpen and Document_Open subs. I put them in Normal.dotm, both in a module and in ThisDocument.
Working well means displaying the correct number of pages in the field.
Here are the codes I tried (they work well when linked to a button):
ActiveDocument.Fields.Update ' ###### END OF CODE 1 Dim aStory As Range Dim aField As Field For Each aStory In ActiveDocument.StoryRanges For Each aField In aStory.Fields aField.Update Next aField Next aStory ' ###### END OF CODE 2 Dim oStory As Range For Each oStory In ActiveDocument.StoryRanges oStory.Fields.Update If oStory.StoryType <> wdMainTextStory Then While Not (oStory.NextStoryRange Is Nothing) Set oStory = oStory.NextStoryRange oStory.Fields.Update Wend End If Next oStory Set oStory = Nothing ' ###### END OF CODE 3
I even tried selecting the specific field like I do manually by using a bookmark and the following code:
ActiveDocument.Bookmarks("PgNum").Range.Fields(1).Result.Select Selection.Fields.Update
Not working well means:
The above codes update the field, but instead of updating with the number of pages (which is 7) it just updates to 2. Each above code does the same.
I found a workaround by inserting the number of pages in the bookmark field each time I open a word document containing that bookmark field. But the end user isn’t going to do bookmark fields in future documents, so I need that fields to update whenever a word document is opened.
Is there something that I am missing?
Credits to this page (user Bond): https://stackoverflow.com/questions…
I put the following codes into Normal.dotm – ThisDocument, so they are available in any document:
Now while the document opened still with numpages 2 instead of 7, when I clicked Save numpages changed to 7 which is the correct value.
Can’t seem to be able to mark this as solved myself, so if someone with powers would do that it would be great. Thanks.
message edited by Mrrrr