none
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.

    Awais
    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.
    /Jesper www.idippedut.dk
    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?

    Awais
    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)
            {
                try
                {
                    DocumentFormat.OpenXml.Packaging.WordprocessingDocument wr =
                    DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(fileName, true);
                   
                    XmlDocument xdoc = new XmlDocument();
                    string str = wordAPP.ActiveDocument.WordOpenXML;

                    xdoc.LoadXml(str);
                    XmlNamespaceManager nsmanager = new XmlNamespaceManager(new NameTable());
                    nsmanager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                    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));
                    xdoc.Save(stWriter);
                    wr.Close();
                   
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }

            }


    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.

    Awais
    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: http://blogs.msdn.com/ericwhite/archive/2008/09/29/the-flat-opc-format.aspx. 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 ?

    Thanks,

    Rupesh

    Friday, December 2, 2011 6:41 PM