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

℡╲_俬逩灬. 提交于 2019-12-13 15:19:01

问题


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?


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/8560753/update-toc-in-docx-document-using-documentformat-openxml-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!