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)?
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.
Update all the formulas in a documentPress CTRL+A. Press F9.
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).
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.
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.
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With