Page Number Based Fields Update Inconsistently?

Microsoft Office 2013, Word 2013, Windows 7

I've got a 200+ page document with 40 chapters, etc.    There are two sections in the document.  Section 1 contains preface material and the table of contents.  Section 2 contains all the chapters.  The document will grow over time with new chapters being inserted just about anywhere.  

I'm trying to number each page in the footer, get the first page of each chapter and certain other pages on an odd page, and display the correct page numbers in a table of contents.  Sounds simple!  My problem is that even though  I attempt to force an update of field codes, they do not seem to update consistently.  For example after one update, chapters 20, 25 and 32 may wind up beginning on even pages.  After the next forced update, without changing anything in the document, some of those chapters may wind up on odd pages and a different set beginning on even pages.

Each chapter is ended with a page break. The first item on the next page is the following field...

This code is immediately followed by my new chapter header.

The intent of the code is if this is an even page, then print a blank page message and insert a page break prior to the new chapter header.  This code exists about 60-80 times in the document.  Here's an image of a result that worked.  I included the page number in the message in an attempt to troubleshoot the problems.

I wrote VBA code to attempt to force the updates.  It first updates the fields in the main text, followed by the headers and footers, and finally the table of contents.  I'm a newbie with VBA  for Word so I'm probably missing a lot!  Here's the code which I cobbled together from multiple sources:

            

Sub updateFieldsIncludeHeadersFooters()
    Dim sec As Section
    Dim hdrftr As HeaderFooter

    ActiveDocument.Fields.Update 'address the fields in the main text story
    If ActiveDocument.Fields.Update = 0 Then
 MsgBox "Update Successful"
Else
 MsgBox "Field " & ActiveDocument.Fields.Update & _
 " has an error"
End If

    'now go through headers/footers for each section, update fields per range
    For Each sec In ActiveDocument.Sections
        For Each hdrftr In sec.Headers
            hdrftr.Range.Fields.Update
        Next
        For Each hdrftr In sec.Footers
            hdrftr.Range.Fields.Update
        Next
    Next

    If ActiveDocument.TablesOfContents.Count = 1 Then _
     ActiveDocument.TablesOfContents(1).Update

End Sub

The following image shows a field that didn't appear to update...  The "Page 170..." message is clearly on page 167.

After this page it appears that many (if not all) of the fields following it didn't update either.  Right clicking the individual fields and Update works.

Any ideas, quidance?  Thanks much.

John Ashley

The message you are putting on the blank page does not appear to be in a header or footer.

I generally use variations on the Macro by Graham Mayor on this page.

Installing Macros


Volunteering to "pay forward" the help I've received in the Microsoft user community.


Charles Kenyon
Sun Prairie, Wisconsin
wordfaq[at]addbalance[dot]com

Legal site: https://addbalance.com

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Charles, thanks for the input.

You are correct...  the blank page message is in the main body text at the top of each new chapter's first page.

I tried Graham Mayor's code and added the TOC update, so my VBA now looks like the following.  The behavior hasn't changed... it appears as if the update stops functioning part way through the main body and where it stops isn't repeatable.

         

Sub UpdateAll()
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

    If ActiveDocument.TablesOfContents.Count = 1 Then _
     ActiveDocument.TablesOfContents(1).Update

End Sub

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

If you want particular pages of your document to start on an Odd page, it is better to immediately precede them with an Odd Page Section Break.

The way you are trying to do it at the moment might work if you can cause the fields in the document to update starting with the first field in the document and working forwards to the last field.  I am not sure however that is the way in which fields in the document are updated.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thanks Doug,  

The odd page section break is the approach we started with.  We abandoned that when we couldn't figure out how to get it to print the needed blank page message on the even pages.

Your second point about the order in which fields within the main body are updated may be the crux of the issue.  I have no clue about the default order, much less how to impose a sequence.

John Ashley

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

You say:

"Each chapter is ended with a page break. The first item on the next page is the following field...

This code is immediately followed by my new chapter header."

IMHO, you'd do better to not use manual page breaks. Instead, format the Style used for the Chapter headings with the 'Page break before' attribute and, at the end of each chapter, use a field coded as:

{IF{=MOD({PAGE},2)}= 1 "{QUOTE 12}Page {PAGE} intentionally left blank"}

You should be able to manage the updates with code no more complicated than:

Sub UpdateFields()
Application.ScreenUpdating = False
With ActiveDocument
  .Fields.Update
  .PrintPreview
  .ClosePrintPreview
End With
Application.ScreenUpdating = True
End Sub

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Paul,  thanks.  

The VBA runs, but the behavior of the page breaks is still "strange".  

To try to answer the question, "Do all the field codes get executed when running the VBA?" I've modified the field codes so that it prints a time stamp regardless of whether it is an even or odd page.  It appears that all the codes are executed each time the macro runs, but still many times the following still happens:

(1)  some field code displayed page numbers don't always match the footer page numbers

(2)  the blank page message gets printed on some odd numbered pages (with an even number in the blank page message)

(3) and some first pages of chapters get placed on even pages without any blank message getting printed.

I did a quick check of using the "Page break before" attribute and the modified field code and it didn't appear to help, but I can't claim to have checked it exhaustively yet.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Your description suggests that, in addition to the page breaks there may be some other Section breaks in the document. And yes, .Fields.Update updates all fields in the body of the document.
Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 441 Applies to: