Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
scenario: update Table of content using OpenXML SDK 2.5

I will be adding contents in between the document using OpenXML/c#. TOC is already present in the document, I just want to update it programmatically. How to do it ?

Thanks in advance :)

What I have tried:

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

I update existing TOC using this, But when user opens the document, gets a popup(Would you like to update fields ...like this).

How to suppress that ?(I've gone through MSDN links. It seems to be risky) else, is there any alternative like fieldupdate thing to update TOC while generating document?
Posted
Updated 17-Oct-22 17:34pm
Comments
Barry_Sharp 15-Jan-18 4:34am    
First, you cannot suppress that MS Word message. When you set "UpdateFieldsOnOpen" it will mark the element with an "isDirty" flag which will result in MS Word asking for a confirmation before updating it.

Second, if you plan to use the below suggested macro approach, I think it would be far easier for you to have that macro already available in your template document, rather then trying to create it with OpenXML SDK.
If you must use OpenXML SDK for this then you'll need to:
- Change the format from DOCX to DOCM, with "WordprocessingDocument.ChangeDocumentType".
- Create and add "VbaProjectPart", with "WordprocessingDocument.AddNewPart".
- Create and add "VbaDataPart"...

This last requirement is a tricky part, you see inside the DOCM file macros are stored in "vbaproject.bin". That is a binary file which is completaly different to OOXML format, see Office VBA File Format Structure.

Last, as an alternative you may want to check out this Word processing library for .NET Framework. It can enable you to easily manipulate with Word files in C#. For example, see how to create and update Word TOC in C# and VB.NET.

Below code will work for update the table of content automatically with Macro popup

DocumentSettingsPart settingsPart = mainPart.Document.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
                // Create object to update fields on open
                UpdateFieldsOnOpen updateFields = new UpdateFieldsOnOpen();
                updateFields.Val = BooleanValues.True;
                // Insert object into settings part.
                settingsPart.Settings.PrependChild<UpdateFieldsOnOpen>(updateFields);
                settingsPart.Settings.Save();
 
Share this answer
 
Comments
Sathya5995 13-Mar-18 7:41am    
I tried to do it without popup, but it's not possible.
Only easier solution to achieve this is "Macro".
Since these page numberings are taken care by the word processor rendering engine, we cannot predict or add page number programmatically.

It's possible to suppress the Macro pop up, but it's hectic because you've hit registry level which is harmful to the computer. So I'd suggest you accomplish it with macro.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900