none
WordML/OpenXML: Is it possible to create a hyperlink to an internal mime type part? RRS feed

  • Question

  • I'm using the OOXML V2 SDK to generate & assemble Word documents (.docx) from a variety of media sources. 

    Correctly adding/formatting text and image content has not been an issue.

    First Question:

    Is it possible to add an arbitrary file (binary or otherwise) to a WordML package and then use a Hyperlink through an "Internal" relationship to "link" to that file?

    Example:

      1) Add the following files within a package
          /word/attachments/doc1.pdf
          /word/attachments/doc2.doc

      2) Then Add a relationship something to this effect in /word/_rels/document.xml.rels
           <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="/word/attachments/doc1.pdf/" TargetMode="Internal" />
     
      3) Finally reference rId4 from a hyperlink in the document body?

    In my trial, MSWord 2007 indicated that the hyperlink that used rId4 was invalid and wouldn't open the document.   I didn't see anything in the specification that would indicate that internal hyperlinks aren't a valid construct.

    Maybe I'm doing it wrong?


    Alternate solution?

    Since I have a various third party documents in all the usual binary formats like (PDF, Doc, etc, even .exe) that need to be stored within the docx and linked into the document's content.

    It seemed like using the EmbeddedObject / Shape / OleObject would be the way to go here.

    However, I couldn't find a way to create the embedded parts within the package so that they would be recognized as an OLE embedded object.  The word/embeddings/oleObjectX.bin files lacked the necessary binary wrapper that allows them to be treated as embedded objects in Word 2007.  

    For instance, the following seems to assume that you already have the binary data wrapped in the proper OLE compound file. 

            public EmbeddedObjectPart AddEmbeddedObject(WordprocessingDocument doc, Stream stream)
            {
                EmbeddedObjectPart ePart = doc.MainDocumentPart.AddEmbeddedObjectPart("application/vnd.openxmlformats-officedocument.oleObject");
                ePart.FeedData(stream);
                return ePart;
            }
    
    I'm referencing the new embedded object part something like this:

                new EmbeddedObject(
                    new Shape(	new ImageData() ),  // details omitted here
                    new OleObject()
                    {
    Type = OLEValues.Embed, DrawAspect = OLEDrawAspectValues.Content,ProgId = "AcroExch.Document.7",Id = embeddedPartId });
    The result is a properly appearing image that would represent the embedded object, but clicking on it simply acts like it's just a document image.  No Ole activation.


    If anyone has insight into this, I'd appreciate any tips.   I'll follow up with the complete solution if I can find an answer.

    Thanks!

    -Jeff

    Tuesday, April 7, 2009 7:59 PM

Answers

All replies

  • I did it via word app. It seems that the relationship of the oleobject is like this:
     <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject" Target="file:///C:\Documents%20and%20Settings\v-shliao\Desktop\The%20Storage%20of%20Macros%20and%20OLE%20Objects.pdf" TargetMode="External"/>
    the external pdf file is successfully linked to the .docx file, and displayed as an icon. when you click the icon, the pdf file can be opened.
    is this what you want?

    btw, could you tell me what's the purpose of these .docx documents, containing various binary files? also a typical example of how the customers will use these documents, thanks in advance.
    Thursday, April 9, 2009 3:18 AM
  • Thanks,

    Yes, using the Word 2007 application (from the Insert->Object menu) you can select a file of any type and word will wrap that file in a packager object of some sort (Ole2 compound doc?), and store that as a new embedded part in the docx package.  Then it creates an "Internal" OleObject relationship link to that embedded part.  

    So, If you take a simple text file, and Embed it in a Word Document via Word 2007, then look at the ".bin" file it actually stored within the Package, you'll see that word wrapped the original text file in some binary code.

    It seems to me that the OpenXML SDK ought to provide some way of producing these ".bin" files, rather than just assuming that they are already wrapped.

    BTW, You also have the choice to Link to the file via External targetmode (which is what you're showing above).  That's easy and straight forward.

    Why do I want this?

    When migrating from old file formats, (A Lotus Notes document is just one example of many),  my customers want to be able to produce stand-alone Word documents that are as close to identical to the original source as possible.  That includes embedding any files attached/contained in the source.  It's easy to do this using MSWord's GUI, but when migrating hundreds/thousands of documents, being able to do this via OpenXML (preferably via the SDK 2.0+) is essential.

    That said,  It's pretty easy to embed a Part in a Docx package.  Unfortunately, MS-Word requires "Embedded" files that are linked within the main document part to be pre-wrapped as OleObjects in order for Word to recognize any relationship references to these parts. 

    Summary:

    How do I turn an arbitrary file into one of the MSWord required wrapped oleObject ".bin" files??

    It seems that word allows embedded images without requiring any wrapping but doesn't allow a link to embedded files that aren't wrapped.  Well MSWord allows them, but will not activate or provide any indication that such an embedded object actually exists within the package UNLESS it is wrapped as an OleObject (or whatever it's wrapped as).

    Is this more clear?

    Tuesday, April 14, 2009 7:46 PM
  • Thanks for your update. Your understanding is correct. SDK does not provide "wrapper" functionalities, you need to do it yourself. note: image, audio can be handled directly by SDK. 
    Wednesday, April 15, 2009 8:32 AM
  • Thanks,  Good to know.

    So... :) how precisely is that wrapping done?  Is there a spec for what word requires of "wrapped" parts?  What exactly is the file being wrapped with?

    Do you know where I can find some sample code for how to wrap files so that MS Word will recognize them as "launch-able" things? 

    I'm far from the first person to have been begging for some clarity on this issue. :) 

    It seems "not very open" to have a specification that provides a "magic features" that only Microsoft's applications are entitled to take advantage of, so I must be overlooking something. 
    Cheers!

    -J
    Wednesday, April 15, 2009 10:12 PM
  • i don't think there's public resources about how to do the conversion, i guess it's kind of inside logic in word, sorry that i have no knowledge on this. 
    Thursday, April 16, 2009 3:00 AM
  • I have a very similar problem - how to create an ole .bin file from my .avi file such that I can reference it as an object from MS Office applications (namely word and powerpoint)? Have you found any solution to this? Any help would be appreciated.

    Thanks. 
    Tuesday, June 9, 2009 6:01 AM
  • Has anyone found a way to do what JHare asked in this "Summary" section?

    I am also trying to embed any file type into a DOCX file.  It appears that WS Word will only active the OLE object if the file is a binary source. 

    For example, I embedded a TXT file, when you click on the image representing the OLE object, nothing happens, MS Word thinks it is just an image.  HOWEVER, if I import a DOC file (which is binary), I can double click the image that represents the OLE object and it will open that file.

    Thanks for you help.
    Garry
    Wednesday, March 10, 2010 1:03 AM
    • Marked as answer by Bruce Song Wednesday, March 23, 2011 2:49 AM
    Friday, May 28, 2010 4:18 PM