Strange Problem with PAGEREF and Table of Contents in Word

S

sheqman

I have a program that takes data from an Excel spreadsheet and inserts
into a series of word documents using a mailmerge powered by VBA. I
should note that the mailmerge documents are created from templates.
(i.e. the program creates a document from a word template, then applies
the mail merge to that document and outputs the merged document as a
new document which then gets saved.)

Everything works ok except for one legal document that contains a
Word-generated Table of Contents. The original template has the TOC and
updates it when the document opens. However, when a new document is
created based on this template, the TOC in the new document gets
corrupted. Basically, it creates PAGREF field codes for each entry in
the TOC and loses the TOC functionality. This wouldn't be a problem
except that I have another script that takes the merged word docs and
outputs them to PDF. When the script encounters the document in
question, it replaces the PAGEREF codes with "Error! Bookmark not
defined." Obviously this make the resulting document unusable.

Anyway to get around this problem?

Thanks in advance for your help,

Rasheq
 
P

Peter Jamieson

The problem occurs during the merge and is caused by the fact that Merge
destroys the ToC field, leaving the underlying references to the pages that
headings and/or TC fields are on (either PAGEREF or HYPERLINK fields,
depending on the switches in the ToC field) and destroys all bookmarks, so
the "targets" of the PAGEREF or HYPERLINK fields also disappear.

It's difficult to know what you could do about this in a generic automated
merge unless the merge knows that it has to do something special for the one
document with a ToC. Logically speaking, what you probably need to do is
a. detect whether or not there is a ToC
b. If there is, save all the information necessary to recreate the ToC (ToC
switches, location, and perhaps style info. related to the various ToC
paragraph styles)
c. replace the ToC by some kind of placeholder text, or otherwise remember
its position, during the merge
d. After the merge, re-insert the ToC field, restore any other settings
such as paragraph style settings, and re-execute the ToC field

Alternatively, you could do something like get the users to put the ToC
field in an INCLUDETEXTed document, do the merge, and re-execute that field,
but there are various possible gotchas with that approach.

Peter Jamieson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top