locked
Merging XML files exported by Word 2003 RRS feed

  • Question

  • What is the best way to programmatically merge XML files that were exported by Word 2003 with C# and OpenXML and save the result as a docx file?

    I would prefer to do it all in memory if I can and have each file start a new page in the docx file.

    Thanks in advance.

    Tuesday, July 6, 2010 11:12 AM

Answers

  • Hi!

    I'm not sure you want something special, so I'll go with the universal way I'd do this. :) I think you can just create a new document, and write the old ones (the xml and the odt) into it, then save the result. Here's the code for this:

    var word = new Application();
    
    string newfilename = @"C:\...\MergeResult.docx";
    string doc1name = @"C:\...\alma.xml";
    string doc2name = @"C:\...\béka.odt";
    WdBreakType pageBreak = WdBreakType.wdPageBreak;
    
    try
    {
     Document doc = word.Documents.Add();
     Selection selection = word.Selection;
    
     selection.InsertFile(doc1name);
     selection.InsertBreak(pageBreak);
     selection.InsertFile(doc2name);
    
     doc.SaveAs2(newfilename, WdSaveFormat.wdFormatDocumentDefault);
    }
    finally
    {
     word.Quit();
    }
    

    I did this using C# 4.0 and Office 2010, but the logic is mainly the same for Office 2007.

    Hope I could help,

    David

    • Edited by David Fulop Tuesday, July 6, 2010 8:51 PM Forgot to delete the paths of the documents.
    • Proposed as answer by David Fulop Tuesday, July 6, 2010 9:16 PM
    • Marked as answer by Figo Fei Tuesday, July 13, 2010 7:58 AM
    Tuesday, July 6, 2010 8:49 PM

All replies

  • Hi!

    I'm not sure you want something special, so I'll go with the universal way I'd do this. :) I think you can just create a new document, and write the old ones (the xml and the odt) into it, then save the result. Here's the code for this:

    var word = new Application();
    
    string newfilename = @"C:\...\MergeResult.docx";
    string doc1name = @"C:\...\alma.xml";
    string doc2name = @"C:\...\béka.odt";
    WdBreakType pageBreak = WdBreakType.wdPageBreak;
    
    try
    {
     Document doc = word.Documents.Add();
     Selection selection = word.Selection;
    
     selection.InsertFile(doc1name);
     selection.InsertBreak(pageBreak);
     selection.InsertFile(doc2name);
    
     doc.SaveAs2(newfilename, WdSaveFormat.wdFormatDocumentDefault);
    }
    finally
    {
     word.Quit();
    }
    

    I did this using C# 4.0 and Office 2010, but the logic is mainly the same for Office 2007.

    Hope I could help,

    David

    • Edited by David Fulop Tuesday, July 6, 2010 8:51 PM Forgot to delete the paths of the documents.
    • Proposed as answer by David Fulop Tuesday, July 6, 2010 9:16 PM
    • Marked as answer by Figo Fei Tuesday, July 13, 2010 7:58 AM
    Tuesday, July 6, 2010 8:49 PM
  • Hi,

    Thanks for you assistance.  I have finish up doing the fid/replace work in the xml template files and saving them and then using code like above to insert them into a word template file.

    Only thing not working now is part of the footer substitution of the file name (with out the extension)

    Regards

    Wolf

     

    Tuesday, July 13, 2010 8:11 AM