none
Update toc in 2010 RRS feed

  • Question

  • HI,

    I need to update TOC or create a new TOC  for docx  file .can any one send me the exact code  .pls dont send any links.

    i will copy a a Base document to another file and do some process  and need to update doc  for the final generated docx file.

    i tried  these  steps

    i wrote a Auto open macro to update toc in base document and made the file as docm  and then copied 

    file to another file but the macros is not copying .

    here is my code

     File.Copy(sFileName, sFilePath);
                        ImportCustomUI(sFileName, sFilePath); static void ImportCustomUI(string templateFile, string outputFile)
            {
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(templateFile, true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;

                    RibbonExtensibilityPart customRibbonPart =
                    myDoc.GetPartsOfType<RibbonExtensibilityPart>().First();
                    ExtendedPart vbaPart = null;

                    foreach (IdPartPair partPair in mainPart.Parts)
                    {
                        if (partPair.OpenXmlPart.RelationshipType == "http://schemas.microsoft.com/office/2006/relationships/vbaProject")
                        {
                            vbaPart = (ExtendedPart)partPair.OpenXmlPart;
                            break;
                        }
                    }

                    AddCustomUIParts(outputFile, customRibbonPart, vbaPart);
                }
            }
            static void AddCustomUIParts(string filename, RibbonExtensibilityPart customRibbonPart, ExtendedPart vbaPart)
            {
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filename, true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;

                    if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                        myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

                    myDoc.AddPart<RibbonExtensibilityPart>(customRibbonPart);

                    ExtendedPart extendedPart = null;

                    foreach (IdPartPair partPair in mainPart.Parts)
                    {
                        if (partPair.OpenXmlPart.RelationshipType ==
                        "http://schemas.microsoft.com/office/2006/relationships/vbaProject")
                        {
                            extendedPart = (ExtendedPart)partPair.OpenXmlPart;
                            break;
                        }
                    }

                    if (extendedPart != null)
                        mainPart.DeletePart(extendedPart);

                    if (vbaPart != null)
                        mainPart.AddPart<ExtendedPart>(vbaPart);
                }
            } 

    iam getting error in coz tat code belongs to word 2007 it throws error in sequence has no elements

    please help me to code  for TOC.

    Thursday, September 13, 2012 10:34 AM

Answers

  • It is not possible to create a fully updated TOC in a Word document using Open XML. Updating a TOC requires the document to be opened in the Word application OR you have to use Word Automation Services (you can look that up, since you don't want any links).

    The most you can do using Open XML is to insert a TOC field, but it will not display the information until it has been updated in one of the above-mentioned ways.

    OR you have to generate the entire content of the TOC yourself, by analyzing the content of your document. But there won't be any way to calculate page numbers...


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 13, 2012 12:04 PM
    Moderator
  • Word Automation Services is a service application in SharePoint 2010:
    http://msdn.microsoft.com/en-us/library/ee558830.aspx

    So, this means you will need SharePoint 2010 installed/available in the production enviroment to use this approach.

    Yes, this a very common problem in the area of programmatic Document Generation. Note that Word documents ar "Flow" documents and not "Fixed" (like XPS, PDF) which means that pages are created on the fly when a document gets loaded in an application such as MS Word. The problem here is that Open XML SDK is merely a parser for document files and it doesn't provide functionalities such as layouting and pagination. Maybe another solution would be to use a 3rd party libraries (that are capable of pagination) but I'm not aware of any such library.

    Friday, September 14, 2012 7:46 AM

All replies

  • It is not possible to create a fully updated TOC in a Word document using Open XML. Updating a TOC requires the document to be opened in the Word application OR you have to use Word Automation Services (you can look that up, since you don't want any links).

    The most you can do using Open XML is to insert a TOC field, but it will not display the information until it has been updated in one of the above-mentioned ways.

    OR you have to generate the entire content of the TOC yourself, by analyzing the content of your document. But there won't be any way to calculate page numbers...


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 13, 2012 12:04 PM
    Moderator
  • Hi,

    Thanks  for your quick reply.Word automation services means interop excel i guess?we should not use inter op excel in our project.can u send the code in open XML  for copying a macro and content of the document to another file?now am trying using macros to update TOC?

    it is very urgent in my project can u help me?

    In my project i will generate a document by copying the content from the base document and when i open the document TOC should get update


    • Edited by nikitha.P Friday, September 14, 2012 3:35 AM Change
    Friday, September 14, 2012 3:33 AM
  • Word Automation Services is a service application in SharePoint 2010:
    http://msdn.microsoft.com/en-us/library/ee558830.aspx

    So, this means you will need SharePoint 2010 installed/available in the production enviroment to use this approach.

    Yes, this a very common problem in the area of programmatic Document Generation. Note that Word documents ar "Flow" documents and not "Fixed" (like XPS, PDF) which means that pages are created on the fly when a document gets loaded in an application such as MS Word. The problem here is that Open XML SDK is merely a parser for document files and it doesn't provide functionalities such as layouting and pagination. Maybe another solution would be to use a 3rd party libraries (that are capable of pagination) but I'm not aware of any such library.

    Friday, September 14, 2012 7:46 AM