none
Open XML and DOCX questions

    Question

  • I am using Open XML to work with docx files.  The case in this scenario is as follows.  I have a word ribbon with a button, when the button is clicked the following occurs, first a docx file on a server is opened and the XML of its contents (the entire package with all parts) is obtained, a specific Content Control from within the documnet is removed from the XML, thus leaving me with the XML of a correct Word XML file (package) that can be inserted into the document without error.  The problem is, some additional information needs to be added to the XML before it is inserted into the document.  An XSLT transformation happens on some additional information creating the XML markup for the other information.  This markup is also a complete package.  If i use Selection.InsertXML with either of the two XMLs it works fine, the problem is, this particualr method needs toreturn a single string to a calling function so some additional work can be performed before calling InsertXML one time.  Thus what i am left with is two XDocument objects containing valid package XML that i am not sure how to combine.  They both can have images or hyperlinks in them so i cannot simply get the body contents of one and insert into the body contents of the other as the links (images, hyperlinks, fonts, styles, etc) won't copy over with it.  I thought about using AltChunks, but that works on DOCX files opened as a package, and what i have is the XML for a Word XML file rather than a DOCX file.  Any thoughts or has anyone else needed to do this?

    Thanks, JLinker

    • Moved by Tom_Xu_WXModerator Monday, December 10, 2012 9:18 AM Word development issue (From:Open XML Format SDK)
    Thursday, December 06, 2012 8:41 PM

Answers

  • Hi JLinker

    I've never tried to do this with the "Package" format, although I have experimented a bit with the analagous problem with the full files. altChunk has some weaknesses: it doesn't fully integrate all header and footer information (loses the setting for Different First Page, for example), so I looked at what it would take to do the integration "from scratch".

    Put simply, there are no shortcuts. You have to have a thorough understanding of how Word documents and, in your case, flat file packages are constructed. Your code would have to check each file to be inserted into a target package for special things (such as hyperlinks, images, fonts, etc.) and integrate these correctly into the target package. If such an element is not already present, besides inserting the information in the correct place you'd also need to create the Relationship.

    I found that careful inspection of the structures of a document (or documents) that contain what I want helped, supplemented with research into the Open XML file format documentation and schemas. And, of course, it's best to work step-by-step, concentrating on one thing at a time.

    Working with the flat-file format isn't really a much-discussed topic on this forum as the forum mainly targets the SDK, which (unfortunately!) knows nothing about flat-files. You might also look around and ask at OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 10, 2012 7:22 AM

All replies

  • Hi JLinker

    I've never tried to do this with the "Package" format, although I have experimented a bit with the analagous problem with the full files. altChunk has some weaknesses: it doesn't fully integrate all header and footer information (loses the setting for Different First Page, for example), so I looked at what it would take to do the integration "from scratch".

    Put simply, there are no shortcuts. You have to have a thorough understanding of how Word documents and, in your case, flat file packages are constructed. Your code would have to check each file to be inserted into a target package for special things (such as hyperlinks, images, fonts, etc.) and integrate these correctly into the target package. If such an element is not already present, besides inserting the information in the correct place you'd also need to create the Relationship.

    I found that careful inspection of the structures of a document (or documents) that contain what I want helped, supplemented with research into the Open XML file format documentation and schemas. And, of course, it's best to work step-by-step, concentrating on one thing at a time.

    Working with the flat-file format isn't really a much-discussed topic on this forum as the forum mainly targets the SDK, which (unfortunately!) knows nothing about flat-files. You might also look around and ask at OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 10, 2012 7:22 AM
  • Hi JLinker,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that you issue isn't a Open Xml issue. You issue is a Word development issue instead. I will move you thread to Word developmen forum for better support.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 10, 2012 9:16 AM
  • Hi Tom

    Actually, the issue is about Open XML - absolutely no question about that. It's not about the SDK, that's all.

    There's some question in my mind whether the Word for Developers forum is really the right place for this as, up until now, there are no discussions in that forum dealing with the Open XML file formats...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 10, 2012 2:46 PM
  • My solution to my problem ended up not being a solution to this question.  If i have to WordML's in XDocument i still do not know how to combine them.  In my scenario, the XSLT that was called was adding an image to the WordML in the XDocument.  Since the image is static I just go ahead and insert it into my original document, then i call my method that inserts WordML into the document to insert the first XDocument, then I call it again for the second one.  I would still ove to know if there is a way to take two XDocument objects that are Valid WordML and combine them without having to know every possible type of object Word can handle (Images, fonts, drawings, etc).  But i have moved on from this for now.


    Thanks, JLinker

    Thursday, May 02, 2013 9:35 PM