Fun With Word, or When is a Field not a Field

Filed under Uncategorized

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.

image

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:

  1. Create 3 HEADING1 lines
  2. Create a TOC, you should see 3 entries in the TOC, one for each heading 1 line, all pointing to page 1.
  3. Put page breaks between each heading 1 line, to force them to separate pages.
  4. Save the doc. The TOC page numbers don’t change.
  5. 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.

2 Comments

  1. Ralf says:

    The disciple approached the master with a problem.

    "Master, the table of contents in my document does not work correctly. Can you advise me?"

    "My son, consider: what purpose does the table of contents serve?"

    "It describes what the document contains, and in what order."

    "And how does this differ from the document itself? Why this redundancy, oh my child?"

    "It provides the reader with a quick understanding of what the document contains."

    "But to achive full knowledge, must not the document be consumed in its entirety?"

    "Yes, master, but–"

    "And is not a full understanding more beneficial than a brief, superficial understanding?"

    "Yes, master, I see, however–"

    "And would the reader, having read and comprehended the document in its fullness, then turn to the table of contents for even greater understanding?"

    The disciple sighs. "No, master. I suppose not."

    "Then I wonder why such a construct is necessary at all. To understand the fullness of a thing–"

    [The disciple angrily shuts down Microsoft Office 2010 Helpful Agent 1.0 and moves on to Google "Word TOC Problem"…]

  2. Darin says:

    You are seriously twisted.

    And I’m getting flashes of "Big Trouble in Little China"

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*