none
How to append a opened open XML document?

    Question

  • Hello Everyone,

    I have one problem where i need help.

    I have an opened openxml document (ABC.docx) in Word 2007. Lets say, first paragraph is having style "myStyle1" like this in document.xml [MainDocumentPart.Document]

    <w:p><w:pPr><w:pStyle w:val="myStyle1"/></w:pPr><w:r><w:t>My Home</w:t><w:r></w:p> in document.xml.

    Now pragmatically, i have to change the paragraph property style to "myStyle2" based on some calculations in document.xml. This has already achieved.

    But here i am getting problem to append this information to the opened openxml document and have to show the user the style changes immediately from "myStyle1" to "myStyle2". Remember, i am trying to replace the document.xml <w:body> contents with my new changes. I am just replacing the paragraph style names and rest of the content remains same in the document.xml stuff.

    Please let me know, how to do that in C#.

    Thanks,


    Shahab Abbasi

    Thursday, August 01, 2013 12:44 PM

Answers

  • Hi Shahab

    Here's a simple code sample (VBA) that demonstrates the user of InsertXML. The OPC flat file is coming from the WordOpenXML and I've just replaced a bit in the XML to change from Heading 1 to Heading 2 style. This is just to demonstrate the principle.

    I think this article might help you further with what you're trying to do, at a production level

    http://msdn.microsoft.com/en-us/library/office/ff191178(v=office.14).aspx

    Sub ChangeParaStyleWordOpenXML()
        Dim rngO As word.Range
        Dim sXML As String
        sXML = Selection.Range.WordOpenXML
        sXML = Replace(sXML, "w:pStyle w:val=" & Chr(34) & "Heading1" & Chr(34), "w:pStyle w:val='Heading2'")
        Set rngO = ActiveDocument.Paragraphs(2).Range
        rngO.InsertXML sXML
    End Sub
    


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 02, 2013 2:09 PM
  • Thanks Cindy,

    I have made some changes and added needed styles to the styles.xml, then it starts showing the styles there. When the style is not defined , word automatically deletes that.

    My code is working fine now...

    Thanks,


    Shahab Abbasi

    Saturday, August 03, 2013 7:37 AM

All replies

  • Hi Shahab

    This isn't something you can do with the Open XML SDK, it's part of the Word object model. It would require the InsertXML property of the Range object.

    Valid Word Open XML for InsertXML requires the Word Open XML to be the OPC flat file format. You can't simply use the xml snippet you show, it has to be a full file format. You can see this if you look at the result of Range.WordOpenXML (the counterpart for reading the Word Open XML).


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, August 01, 2013 4:08 PM
  • Thanks for the reply.

    I have already created OPC flat file format. Now please let me know, how to use that file to reflect the changes. Do i need to insert the XML retrieved from the OPC flat file to Word Document range using range.insertXML() method? or is there any other ways to do that?

    Thanks, 


    Shahab Abbasi

    Friday, August 02, 2013 10:42 AM
  • Hi Shahab

    Here's a simple code sample (VBA) that demonstrates the user of InsertXML. The OPC flat file is coming from the WordOpenXML and I've just replaced a bit in the XML to change from Heading 1 to Heading 2 style. This is just to demonstrate the principle.

    I think this article might help you further with what you're trying to do, at a production level

    http://msdn.microsoft.com/en-us/library/office/ff191178(v=office.14).aspx

    Sub ChangeParaStyleWordOpenXML()
        Dim rngO As word.Range
        Dim sXML As String
        sXML = Selection.Range.WordOpenXML
        sXML = Replace(sXML, "w:pStyle w:val=" & Chr(34) & "Heading1" & Chr(34), "w:pStyle w:val='Heading2'")
        Set rngO = ActiveDocument.Paragraphs(2).Range
        rngO.InsertXML sXML
    End Sub
    


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 02, 2013 2:09 PM
  • Ok, i tried that but still my problem is not solved: I will try to make my requirement more clear to you.

    Opened Active Open XML Document is having document.xml contents as follows:

    <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
      <w:body>
        <w:p w:rsidR="00ED251F" w:rsidRPr="00ED251F" w:rsidRDefault="00B30A2F" w:rsidP="008723F9">
          <w:pPr>
            <w:pStyle w:val="DocumentNumber"/>
            <w:numPr>
              <w:ilvl w:val="8"/>
              <w:numId w:val="18"/>
            </w:numPr>
            <w:rPr>
              <w:b w:val="0"/>
            </w:rPr>
          </w:pPr>
          <w:bookmarkStart w:id="0" w:name="_elm000001"/>
          <w:bookmarkStart w:id="1" w:name="_elm000011"/>
          <w:bookmarkStart w:id="2" w:name="_elm000000"/>
          <w:r>
            <w:t>Document Number 702-16</w:t>
          </w:r>
          <w:bookmarkEnd w:id="0"/>
        </w:p>
        <w:p w:rsidR="00ED251F" w:rsidRPr="00B30A2F" w:rsidRDefault="00B30A2F" w:rsidP="00ED251F">
          <w:pPr>
            <w:pStyle w:val="SeriesTitle"/>
            <w:rPr>
              <w:b w:val="0"/>
            </w:rPr>
          </w:pPr>
          <w:bookmarkStart w:id="3" w:name="_elm000002"/>
          <w:bookmarkStart w:id="4" w:name="_elm000003"/>
          <w:r>
            <w:t>Product Assurance</w:t>
          </w:r>
          <w:bookmarkEnd w:id="4"/>
        </w:p> </w:body> </w:document>


    Now using OPC flat file, i have replaced the w:pStyle's to my required ones. Like this:

          <w:document xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
            <w:body>
              <w:p w:rsidRPr="00ED251F" w:rsidR="009677D0" w:rsidP="009677D0" w:rsidRDefault="009677D0">
                <w:pPr>
                  <w:pStyle w:val="cover_pubno" />
                  <w:numPr>
                    <w:ilvl w:val="8" />
                    <w:numId w:val="18" />
                  </w:numPr>
                  <w:rPr>
                    <w:b w:val="0" />
                  </w:rPr>
                </w:pPr>
                <w:bookmarkStart w:name="_elm000001" w:id="0" />
                <w:bookmarkStart w:name="_elm000011" w:id="1" />
                <w:bookmarkStart w:name="_elm000000" w:id="2" />
                <w:r>
                  <w:t>Army Regulation 702-16</w:t>
                </w:r>
                <w:bookmarkEnd w:id="0" />
              </w:p>
              <w:p w:rsidRPr="00B30A2F" w:rsidR="009677D0" w:rsidP="009677D0" w:rsidRDefault="009677D0">
                <w:pPr>
                  <w:pStyle w:val="cover_seriestitle" />
                  <w:rPr>
                    <w:b w:val="0" />
                  </w:rPr>
                </w:pPr>
                <w:bookmarkStart w:name="_elm000003" w:id="3" />
                <w:bookmarkStart w:name="_elm000002" w:id="4" />
                <w:r>
                  <w:t>Product Assurance</w:t>
                </w:r>
                <w:bookmarkEnd w:id="3" />
              </w:p>
              </w:body>
    </w:document>

    // Now here is my code: // Using the Open XML SDK 1.0 to process it. wdDoc = WordprocessingDocument.Open(packageStream, true); wdDoc.MainDocumentPart.Document.Save(); // Flush the contents of the package. wdDoc.Package.Flush(); // Convert back to flat OPC by using this in-memory package. // Here i am appending my changes to the OPC flat file in the function OpcToFlatOpc and returned the OPD flat file to XDocument XDocument xDoc = OpcHelper.OpcToFlatOpc(wdDoc.Package); string openxml = string.Empty;

    // Return this string. openxml = xDoc.ToString();

    WdApplication.ScreenUpdating = false; object start = WdApplication.ActiveDocument.Content.Start; object end = WdApplication.ActiveDocument.Content.End; Word.Range range = WdApplication.ActiveDocument.Range(ref start, ref end); string str = string.Empty; str = range.WordOpenXML; str = str.Replace(str, openxml); range.InsertXML(str, ref obj_Missing); WdApplication.ActiveDocument.Save(); WdApplication.ScreenUpdating = true;

    Now when i looked back the changed open xml document's document.xml, i came to know that everything is there, except the <w:pStyle> that i have created. Have a look at the document.xml file generated after insertXML:

    <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
      <w:body>
        <w:p w:rsidR="00ED251F" w:rsidRPr="00ED251F" w:rsidRDefault="00B30A2F" w:rsidP="008723F9">
          <w:pPr>
            <w:numPr>
              <w:ilvl w:val="8"/>
              <w:numId w:val="18"/>
            </w:numPr>
            <w:rPr>
              <w:b w:val="0"/>
            </w:rPr>
          </w:pPr>
          <w:bookmarkStart w:id="0" w:name="_elm000001"/>
          <w:bookmarkStart w:id="1" w:name="_elm000011"/>
          <w:bookmarkStart w:id="2" w:name="_elm000000"/>
          <w:r>
            <w:t>Army Regulation 702-16</w:t>
          </w:r>
          <w:bookmarkEnd w:id="0"/>
        </w:p>
        <w:p w:rsidR="00135C71" w:rsidRPr="00B30A2F" w:rsidRDefault="00135C71" w:rsidP="00414EA5">
          <w:bookmarkStart w:id="4" w:name="_elm000003"/>
          <w:bookmarkStart w:id="5" w:name="_elm000002"/>
          <w:r>
            <w:t>Product Assurance</w:t>
          </w:r>
          <w:bookmarkEnd w:id="4"/>
        </w:p>
        <w:bookmarkEnd w:id="5"/>
    </w:body>
    </w:document>

    At the time of insertion all the styles were there, but after the insertXml, every style is gone. I dont know, what is going wrong there. Please have a look at that?

    Thanks,



    Shahab Abbasi

    Saturday, August 03, 2013 6:34 AM
  • Thanks Cindy,

    I have made some changes and added needed styles to the styles.xml, then it starts showing the styles there. When the style is not defined , word automatically deletes that.

    My code is working fine now...

    Thanks,


    Shahab Abbasi

    Saturday, August 03, 2013 7:37 AM
  • Hi Shahab

    Glad you've got it working :-) Yes, the style must be present in the package part defining the style definitions, otherwise Word is not "happy"!


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, August 03, 2013 12:36 PM