Update TOC in .docx document using DocumentFormat.OpenXml (C#)

I create new word document using DocumentFormat.OpenXml. I load template document in which located the table of content and the table with picture logo. I append other content using c# code for this template. I send this document to user.

When user get this document and open it - the table of contents not update. I found the resolve this problem. I add to my code

  • mainPart.DocumentSettingsPart.Settings.Append(new UpdateFieldsOnOpen() { Val = true });

But when user will get the document and will open it he get the message from MS Word "You wanna update fields?".

Is there any other way to generate table of contents or update table of contents without any message to user?


I got answer for my question: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/7f53a939-f50e-423d-90fa-1f3c58945a6e

The cause of what you're seeing is a security measure that was introduced a few years ago. Possibly, you can suppress the message by using the Registry entry described in the following KB article. You need to be aware, however, that this could reduce system security.

The following blog article describes the "correct" way to update the TOC: http://support.microsoft.com/kb/330079

Other than that, the only thing available would be a macro embedded in the document or in an addin that uses automation to force the fields to update.

We have had a long discussion in similar thread regarding generating Table Of Content (TOC) with help of OpneXML SDK 2.0. And there is a detailed solution explaining how it could be achieved. Look at this thread for details: How to generate Table Of Contents using OpenXML SDK 2.0?

Need Your Help

Eclipse Indigo and m2e and wsimport goal. how to generate ws files in this year 2012

eclipse maven sts-springsourcetoolsuite m2e wsimport

Am part of a team building a jsp/j2ee webapp. I decided to use STS 2.9.2 based on eclipse Indigo as opposed to every other person in the group as they all use netbeans .Every thing was perfect unti...

XmlHttpRequest Timeout - Property or Method Not Found

com vbscript timeout xmlhttprequest

Attempting to make an http post to a web service from a basicscript (VBScript/VBA scripting language - SummitSoftware) and want to implement a timeout.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.