Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically update formula field after generating Open XML Word document

I have a library that generates Word documents using the OpenXML SDK, one of the functionalities of the library is the generation of tables with formulas on the last row (such as SUM(ABOVE)).

Word has these functions built-in, but when generating the document I have to assume that Word isn't installed on the machine.

The problem is that if I just generate the table and the formula field, it won't be updated automatically when the document is opened, the user would have to open the document and manually select "update field" for the function to be calculated.

Is there any way to do this within the code (without having to calculate the function manually)?

like image 635
willvv Avatar asked Jun 26 '10 02:06

willvv


People also ask

How do you automatically update fields in Word?

Update all fields in a document Tip: To make sure that you don't forget to update your table of contents before you print the document, set Word to update fields automatically before printing. Click FILE > Options > Display, and under Printing options, select the check box for Update fields before printing.

How do I update all formulas in Word?

Update all the formulas in a documentPress CTRL+A. Press F9.

How do I use the XML mapping pane in Word?

In Word, you open the custom XML part in the XML Mapping pane, and then use the pane to map elements to content controls in the Word document. The XML Mapping pane is accessible from the Developer tab (for more information, see Show the Developer Tab on the Ribbon).

How do I stop Word from automatically updating fields?

The only way to stop Word from updating fields is to lock them. You can do this by selecting the entire document (Ctrl+A) and then pressing Ctrl+F11. The fields will then remain locked until you specifically unlock them by selecting the entire document and pressing Ctrl+Shift+F11.


2 Answers

No, this is by design. See Which fields are updated when you open, repaginate, or print document for details.

This true of any manual input as well (for example if you had 10 in A1, 10 in A2, inserted =SUM(ABOVE) in A3, you'd have 20. But if you changed A1 to 15, A3 would not update automatically unless and until you manually did so).

The only ways around this are to create an add-in (VSTO, VBA, etc.) to sink the Document_Open event on client machines and run some code to update all fields or create an Interop app on the server that opens the doc, renders it with some code and then saves it back before sending it on further.

like image 117
Todd Main Avatar answered Sep 22 '22 16:09

Todd Main


Have you seen this, UpdateFieldsOnOpen class. (https://msdn.microsoft.com/en-us/library/cc861799) It should force an update of all fields on the first time a documented is opened using an app that can update it (such as Word).

I guess that each field that has it's Dirty flag set to true will get an update, but don't quote me on that ;)

like image 21
Kolky Avatar answered Sep 18 '22 16:09

Kolky