i'm sorry, i didn't like your solution proposals as I use docx to not have a need to include the open sdk. (But it took me to this solution 95% of the way, so thanks!)
I found out DocX can actually do that easily with just a few minor changes to the code.
To accomplish that goal add the following lines to DocX.cs into the class:
/// Activate Updating Fields on Open
public void UpdateFieldsOnOpen()
XElement element = new XElement(XName.Get("updateFields", DocX.w.NamespaceName));
element.Add(new XAttribute(XName.Get("val", DocX.w.NamespaceName), "true"));
Finally you need to modify the save process slightly because docx reloads the settings part directly before saving the document.
// Comment out or remove those lines:
- using (TextReader tr = new StreamReader(settingsPart.GetStream()))
- settings = XDocument.Load(tr);
// Insert this instead:
+ if (settings == null )
+ using (TextReader tr = new StreamReader(settingsPart.GetStream()))
+ settings = XDocument.Load(tr);
Use these lines to use the new option:
As a result, word will tell you that some fields need updating when you open the document.
Well after you bypassed that new preview mode that they put into word to complicate usage. Which i know everybody at my workplace loves very much.
It works on my computer :).
I hope it will help YOU ALL TOO since you are great people that deserve TOCs.