Solved Word VBA AutoOpen for Field Updating - not working

Microsoft Word 2013 32/64-bit
January 15, 2019 at 00:43:33
Specs: Windows 7 x64, 2,4 GHz / 4 GB
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):


' ###### END OF CODE 1

Dim aStory As Range
Dim aField As Field

   For Each aStory In ActiveDocument.StoryRanges
      For Each aField In aStory.Fields
      Next aField
   Next aStory

' ###### END OF CODE 2

Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
    If oStory.StoryType <> wdMainTextStory Then
        While Not (oStory.NextStoryRange Is Nothing)
            Set oStory = oStory.NextStoryRange
    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:


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?

message edited by Mrrrr

See More: Word VBA AutoOpen for Field Updating - not working

January 15, 2019 at 22:10:38
✔ Best Answer
Looks like I finally found a different approach, updating fields on document save not on open. This works for me on Office 2013. The document open codes seem to have a problem with 2013. Probably an update would fix, but since I can't install updates I was stuck.

Credits to this page (user Bond):

I put the following codes into Normal.dotm - ThisDocument, so they are available in any document:

Dim WithEvents TheApp As Word.Application

Private Sub Document_Open()
    Set TheApp = ThisDocument.Application
End Sub

Private Sub TheApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
End Sub

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

Report •
Related Solutions

Ask Question