Consider a simple Word document which contains three things:
- A bookmarked portion of the text to be used as a lookup value (bookmark name is a_bookmark)
- A legacy dropdown control to allow user to select one of two options (control bookmark name is a_control, option values are
"One"and"Two", with "One" currently selected) IFfield that analyzes the value of a_control dropdown and generates a fixed text if dropdown has a value of"One", and a lookup value stored in a_bookmark bookmark otherwise.
So the IF field code looks like this:
{ IF { REF a_control } = "One" "Fixed value" { REF a_bookmark } }
Actually, the dropdown is not essential in this scenario, it is only used here to show that the execution of both branches of IF field is theoretically possible. In principle, one can further reduce this example to the following:
{ IF 1 = 1 "Fixed value" { REF a_bookmark } }
So basically we use a lookup value in the branch that will not currently execute. In case of the dropdown this is achieved by the fact that the dropdown currently contains a value of "One" so that the main branch of the IF field is executed. In case of the simplified example this is achieved by virtue of 1 = 1 tautology.
Bottom line is that { REF a_bookmark } will not be currently executed, but it can be executed in principle in future when conditions change.
Finally one more setup: imagine that the text inside of the a_bookmark bookmark has changed for some legitimate reason and we want it to be reflected (updated) in { REF a_bookmark } lookup.
Here is the problem: I can't force the update of the { REF a_bookmark } field either from VBA or from Word no matter what I try. So far I have tried:
- Iterating through the fields in VBA, finding the field of interest and calling its
Updatemethod. No result, itsResult.Textproperty continues to hold some old value. - Updating all fields programmatically:
ThisDocument.Fields.Update - Invoking print preview programmatically:
ThisDocument.PrintPreview(yes, Update fields before printing option is on) - Invoking print preview from GUI
- Expanding the
IFfield entirely in GUI with Shift-F9, then selecting{ REF a_bookmark }field in that expansion and invoking Update field context menu option specifically for that field.
So it seems to me that Word 2007 is just refusing to update a field that is nested into currently passive branch of the IF field, even if I talk to this field directly in VBA. Does anyone know a way to force-update such field?
Update: No, "just wait until the user selects the alternative option and the field will become an active branch and will update on its own" solution is not suitable, the reason I need the field update NOW is because VB script needs to replace that field with its actual value (unlink the field) at the time of running the script, way before the user will be using the document.
(It's a while since you posted, so I expect you found a way to deal with this. But...)
I was able to replicate this problem. I was surprised.
What to do depends partly on what the bookmark might contain.
If it's just a piece of text, then you could start with
then, if you know that field index of that
{ REF a_bookmark }field is 10 (say),use, e.g.
or if it could contain non-text material such as images, then
But, be aware that if
a_bookmarkcontains anything that Word would treat as a double quotation mark, theIFfield containing the{ REF a_bookmark }field will resolve as you might hope, whereas theIFfield containing the content from thea_bookmarkbookmark will be truncated after that mark. i.e. if you had to avoid that you'd have to process the bookmark text to deal with that problem.Also, if you have to use an approach like that, you might be able to avoid having a
{ REF a_bookmark }field in theIFfield in the first place, and just use another bookmark to indicate where you should put the content ofa_bookmark. e.g.but insert a point bookmark called
b_bookmarkbetween the final"".Then, e.g.
or whatever.