Saving underlying OpenXml. RRS feed

  • Question

  • Hello!

    I have a word document opened and i want to modify the underlying openXml parts of it while the document remains open. is it possible?

    any help would be appreciated.

    Friday, March 6, 2009 10:13 AM

All replies

  • Hi,

    jewelthief said:

    I have a word document opened and i want to modify the underlying openXml parts of it while the document remains open. is it possible?

    any help would be appreciated.

    Coincidently I just wrote a reply to a saimilar question on my blog this morning. It seems that you are asking the same thing.

    I don't thing your problem is related to OpenXML or the SDK as such. It has to do with the applicaiton you use and the operating system you use if the file is exclusively locked when opened in whatever application.
    Friday, March 6, 2009 10:43 AM
  • Thanks Jesper! Yes! I had some doubt over what you mentioned that it is not the openXml SDK issue. Anyway! This is what i tried but still stuck at the same problem.

    I got the xml string from  ActiveDocument.WordOpenXml (It gives back the raw one Xml string of whole package) and loaded it into an XmlDocument variable. Extracted the required part using Xpath and modified the it. It goes ok till now.

    The problem comes when i try to save back the modified openXml part. I believe that i have to open the document again (to acess it uderlying XML) using OpenXml SDK and overwrite the modified parts. Understandably it doesnt allow me to open the already opened document. What should be the solution? or am i missing something? or is it about file acess permisions?

    Friday, March 6, 2009 11:01 AM
  • Can you show us the code you are trying to use?

    Zeyad Rajabi
    Zeyad Rajabi (MS)
    Friday, March 6, 2009 7:35 PM
  • Here is the code of what i am trying to do.

     public void Open(string fileName)
                    DocumentFormat.OpenXml.Packaging.WordprocessingDocument wr =
                    DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(fileName, true);
                    XmlDocument xdoc = new XmlDocument();
                    string str = wordAPP.ActiveDocument.WordOpenXML;

                    XmlNamespaceManager nsmanager = new XmlNamespaceManager(new NameTable());
                    nsmanager.AddNamespace("w", "");
                    XmlNodeList nodeList = xdoc.SelectNodes("//w:t", nsmanager);
                    nodeList[0].InnerText = "Hello World";
                    XmlElement ele = (XmlElement)nodeList[0];
                    ele.SetAttribute("id", "1234");
                    System.IO.StreamWriter stWriter = new System.IO.StreamWriter(wr.MainDocumentPart.GetStream(System.IO.FileMode.Create));
                catch (Exception e)


    Now it is obvious that it will throw an exception while opening WordprocessingDocument because the file already opened in Ms word.

    now please if anyone can help me out here.

    Monday, March 9, 2009 8:26 AM
  • Thanks for sharing your code. The main issue is that you are getting your xml from ActiveDocument.WordOpenXML. That method returns the flat version of an Open XML document. For more information take a look at: My recommendation is to preprocess the WordOpenXML string to only contain the infomation you want for the main document part.

    Zeyad Rajabi
    Zeyad Rajabi (MS)
    Tuesday, March 10, 2009 10:13 PM
  • Zeyad,

    Did you get a solution for your issue ? were you able to extract only the required information ? Could you please share the solution ?



    Friday, December 2, 2011 6:41 PM