Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically creating Office Open XML documents: update all fields on open

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

like image 919
avernet Avatar asked Sep 12 '26 03:09

avernet


2 Answers

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">
    <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?"

like image 196
Eric Avatar answered Sep 16 '26 01:09

Eric


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.

like image 26
Dirk Vollmar Avatar answered Sep 16 '26 02:09

Dirk Vollmar