views:

643

answers:

3

I am programmatically generating an Office Open XML document (Word 2007 format), and would like to have all the fields updated when the document I generate is first opened in Word.

When I researched this a while ago, the only way to do this was to run VB Script when the document opens. But VB Script doesn't run by default, and for this to work the user has to authorize scripts, which is more complicated than asking the user to do a "select-all, F9", and so this defeats the purpose.

Any suggestion?

Alex

+1  A: 

Your research already showed the correct results. Using VBA or any other form of Word automation is the only way to go. Word nor OpenXML have an option to automatically update fields on open.

0xA3
@divo Good to know. Thank you for confirming this.
Alessandro Vernet
+1  A: 

There is a way to do this without VB Script.

You can set the field as dirty and it will update when the document is opened.

<w:fldSimple w:instr="TOC" w:dirty="true" />

If you have many fields you can update the settings in your document so that all fields are updated when the document is opened

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"&gt;
    <w:updateFields w:val="true" />
</w:settings>

However, these both have the downside of a confirmation dialog appearing when the document is opened that says "This document contains fields that may refer to other files. Do you want to update the fields in this document?"

Eric
A: 

I have seen this solution on many forums but if I add to the settings.xml I get, "Unspecified error" in the document.xml . It does not appear to be a real way to do this. I can accepted a dialog box but this solution does not work.

YoungGun