none
Direcly Manipulate dotm and docx xml RRS feed

  • Question

  • Hi,

     

    I have a Word 2010 VSTO project. I have an existing template with its own styles, building blocks, numbered lists, etc. I need to merge 5 other word templates in this one. The 5 Word templates are a mix of Word 2007 and Word 2010. The components from the other templates will need to be able to be segregated within the single template. I will rename all of the styles from the 5 source templates before inserting into the main one using some sort of naming pattern to segregation within the VSTO application. I will need to pull in all of the building blocks from the other 5 templates into the main one, renaming them as well. I will need to update all of the (thousands) of word documents associated with the other 5 templates to use the newly named styles and building blocks. The word documents are a mix of Word 2007 and Word 2010.

    I was thinking about attempting to write a process to manipulate the XML for the various templates to achieve these tasks programmatically. I.E. pulling XML from the 5 and inserting into the 1. Does anyone have any experience with directly manipulating the Word template and Word document XML? Is this even a viable solution?

     

    Thanks,

    Chris

    Friday, May 27, 2016 3:25 PM

Answers

  • Hi dc_yager,

    According to your description, Using openXML SDK only, you could use AltChunk element to merge the multiple document into one.

    using (WordprocessingDocument myDoc =
        WordprocessingDocument.Open("Test1.docx", true))
    {
        string altChunkId = "AltChunkId1";
        MainDocumentPart mainPart = myDoc.MainDocumentPart;
        AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
            AlternativeFormatImportPartType.WordprocessingML, altChunkId);
        using (FileStream fileStream = File.Open("TestInsertedContent.docx", FileMode.Open))
            chunk.FeedData(fileStream);
        AltChunk altChunk = new AltChunk();
        altChunk.Id = altChunkId;
        mainPart.Document
            .Body
            .InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
        mainPart.Document.Save();
    }

    For more information, click here to refer about How to Use altChunk for Document Assembly 

    and here to refer about AltChunk class

    Monday, May 30, 2016 5:00 AM