I was investigating some strange behavior with respect to fields in Word and when, exactly, do they get “automagically” updated.
In this particular case, I had a document with a table of contents. The TOC looked completely correct, until the doc was “saved” as a PDF file using the Word 2007 PDF/XPS format plugin. In reality, it doesn’t matter whether the doc is saved as a PDF or printed, Word functions in the same way.
During the save to PDF, the TOC entries were all converted to “Error! Bookmark not defined”. In this case, that was correct. The bookmarks used to create the TOC entries had been removed, but, with the “Update fields” options set to false, I was expecting the fields in the TOC to NOT get updated.
As far as I can tell, they really shouldn’t get updated, but they do anyway. This appears to apply to Word 2000 as well as 2007 (didn’t check all the interim versions).
So, TOC Entry fields appear to not behave by the same rules as other fields. For instance, I embedded an AUTHOR field, save the doc, then changed the Author Document property to something else, then saved as PDF. Lo and behold, the Author Field DID NOT change from it’s original value, meaning that that field type was not “auto updated” before the print/save as PDF.
Then I tried just a plain bookmark. Insert a named bookmark somewhere in the doc, then somewhere else, insert a PAGEREF field (type 37 for those Word VBA masochists) pointing to that bookmark.
The field should take on the value of the page the bookmark is defined on.
Now, delete the bookmark (not the pageref) and print.
Aha! Another “Error! Bookmark not defined”.
It’s not TOC Entries, per se, that have the problem. It’s bookmarks. It appears that even if you tell Word to NOT UPDATE fields at print, any field that references a bookmark, does get updated. And it’s not just that Word checks that the bookmark is defined or not. Word definitely updates the field referring to the bookmark. You can see this if you do the following:
- Create 3 HEADING1 lines
- Create a TOC, you should see 3 entries in the TOC, one for each heading 1 line, all pointing to page 1.
- Put page breaks between each heading 1 line, to force them to separate pages.
- Save the doc. The TOC page numbers don’t change.
- Now save as PDF. The TOC page number DO change at this point.
Looks like a Word bug, albeit a long standing one.
And the workaround?
You have to decide between:
- Leaving the field that refer to the bookmark in the document, but deleting the bookmarks themselves, which will result in an Error.
- Deleting the bookmark AND the field that references it (which would wipe out your TOC)
- Converting all fields that reference bookmarks to text and then deleting the bookmarks (probably the best of the choices)
There’s no easy answer to this one, unfortunately. That last option involves quite a bit of scanning through fields and bookmarks. Interestingly, Bookmarks must have a unique name, so they are pretty easy to get back to and track.
Fields, on the other hand, have no unique ID in Words object model.
They have a “code”, but that can be the same across multiple fields.
They have an “index”, but that can change as you delete fields.
From my vantage, the lack of unique ID’s for many of the elements in the Word Object model is a significant, if not partially understandable, problem.