none
Change (get and set) XML for Word Document using Microsoft.Office.Interop.Word library ? RRS feed

  • Question

  • I build word addin that have two check-boxes to swabbing between TEXT view and XML View. To provide  editing both TEXT and XML View for users.  
    When i want to change the XML I must be close the document and re-open it usingDocumentFormat.OpenXml library. so this take long time and some time not work

    The code :

    private void ShowDocBodyXML_Click(object sender, RibbonControlEventArgs e)
            {
                var doc = Globals.ThisAddIn.Application.ActiveDocument;
                doc.Save();
                string fileName = doc.FullName;
                doc.Close();
                using (WordprocessingDocument document = WordprocessingDocument.Open(fileName, true))
                {
                    MainDocumentPart mainPart = document.MainDocumentPart;
                    Body body = mainPart.Document.Body;
                    string text = body.InnerXml;
                    //do some change to the XML 
                    body.RemoveAllChildren();
                    DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
                    Run run = para.AppendChild(new Run());
                    run.AppendChild(new Text(text));
                }
                Globals.ThisAddIn.Application.Documents.Open(fileName);            
            }

    I want to asking. Is it possible to change XML in doc.WordOpenXML I mean change XML of the document using Microsoft.Office.Interop.Word library rather than using DocumentFormat.OpenXml library I mean there is no need to close and re-open the document? 
    If it is not possible why  Microsoft using Interop.Word library  just provide get method for show XML and didn't provide set method for change XML?


    Thursday, December 31, 2015 6:15 PM

Answers

  • The Range.InsertXML method allows you to write valid Word Open XML to an open document - no need to close the document.

    This works well with any text content; in my experience there are some restrictions (things that don't work) when you try to change number formatting and page layout. (There may be more things that don't work - those are just the ones I've encountered, personally.)

    It's possible to get the WordOpenXML property and change it using standard XML tools. If you want to use the Open XML SDK to do the processing then you need to convert the OPC flat file format of the WordOpenXML to the Zip package file format, then back again to OPC flat file in order to write it back to the document. You'll find code for this here:

    https://blogs.msdn.microsoft.com/ericwhite/2008/09/29/transforming-flat-opc-format-to-open-xml-documents/


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Thursday, December 31, 2015 7:45 PM
    Moderator

All replies

  • I build word addin that have two check-boxes to swabbing between TEXT view and XML View. To provide  editing both TEXT and XML View for users.  
    When i want to change the XML I must be close the document and re-open it using DocumentFormat.OpenXml library. so this take long time and some time not work

    The code :

    private void ShowDocBodyXML_Click(object sender, RibbonControlEventArgs e)
            {
                var doc = Globals.ThisAddIn.Application.ActiveDocument;
                doc.Save();
                string fileName = doc.FullName;
                doc.Close();
                using (WordprocessingDocument document = WordprocessingDocument.Open(fileName, true))
                {
                    MainDocumentPart mainPart = document.MainDocumentPart;
                    Body body = mainPart.Document.Body;
                    string text = body.InnerXml;
                    //do some change to the XML 
                    body.RemoveAllChildren();
                    DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
                    Run run = para.AppendChild(new Run());
                    run.AppendChild(new Text(text));
                }
                Globals.ThisAddIn.Application.Documents.Open(fileName);            
            }

    I want to asking. Is it possible to change XML in doc.WordOpenXML I mean change XML of the document using Microsoft.Office.Interop.Word library rather than using DocumentFormat.OpenXml library I mean there is no need to close and re-open the document?
    If it is not possible why  Microsoft using Interop.Word library  just provide get method for show XML and didn't provide set method for change XML?






    Thursday, December 31, 2015 2:55 PM
  • The Range.InsertXML method allows you to write valid Word Open XML to an open document - no need to close the document.

    This works well with any text content; in my experience there are some restrictions (things that don't work) when you try to change number formatting and page layout. (There may be more things that don't work - those are just the ones I've encountered, personally.)

    It's possible to get the WordOpenXML property and change it using standard XML tools. If you want to use the Open XML SDK to do the processing then you need to convert the OPC flat file format of the WordOpenXML to the Zip package file format, then back again to OPC flat file in order to write it back to the document. You'll find code for this here:

    https://blogs.msdn.microsoft.com/ericwhite/2008/09/29/transforming-flat-opc-format-to-open-xml-documents/


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Thursday, December 31, 2015 7:45 PM
    Moderator
  • Thank you Cindy Meister. Actually Range.InsertXML method it's very useful for me. But as you say there are some restrictions header ,footer ,number formatting and page layout. How can I change those things using OPC flat file format rather than Open XML format. I mean using document object in Microsoft.Office.Interop.Word library.
    Thursday, December 31, 2015 10:28 PM
  • Using the "interop" you can change all those things, yes. This works via the object model and not using XML.

    But those would be different questions. To ask them, I recommend you open a new question for each topic and present specific problems. Also, if your questions are not specific to VSTO, but to Word, I recommend asking in the Word for Developers forum where you'll each Word specialists.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Saturday, January 2, 2016 9:33 AM
    Moderator
  • Thank you very much for your helping....
    Saturday, January 2, 2016 11:28 AM