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