none
Embedding Documents/Object in Word by Using the Open XML SDK RRS feed

  • Question

  • Hi,

    With the help of following link "http://msdn.microsoft.com/en-ca/library/ee355229.aspx" i am able to embed documents in word successfully. After inserting the document as object i have to mention the file name for the object. Is there any way to mention the Object name using code?. 

    And if i try to insert more than one object (different files) in document it shows the second object as image. How to insert more than one object in word document?.

    Thanks in advance :)

    Friday, April 5, 2013 5:42 AM

Answers

  • Hi Joseph

    OK, now I understand your question.

    Looking at the internal XML, I see that
     1. The document is embedded as a binary OLE object, even though it's an Open XML file. This is because "embedding" is an OLE thing, and OLE is binary. 
      2. The caption is not stored anywhere in the file "in the clear". Word must be storing it in that binary information.
      3. When you double-click the embedded image it opens as "Document in [DocumentName]": Word isn't using the caption to label the document when it's opened to be read/edited.

    Theoretically, this would be Word's old binary file format. The specs for this are available to the public; you'll find a link to that information as well as a forum that deals with it here: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/063a23a6-1595-4c83-a25f-6c94658c4649. If you're lucky, you could extract/edit this in the binary...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 8, 2013 7:14 AM
    Moderator
  • Hi Joseph

    1) You've posted this time in the forum for the Word API. Of course, that's the kind of answer you're going to get.

    2) We've been over this topic twice in the Open XML SDK forum and my answer there still stands. The code you see that does affect the caption supports the answer I gave you there: this is stored in the binary OLE. The only way you can access it would be to find out how to read the binary of the embedded file. For information on that you need to ask in the forum that supports the old binary file format.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, April 23, 2013 8:08 AM
    Moderator

All replies

  • Hi Joseph

    I'm not sure I completely understand your questions.

    <<i am able to embed documents in word successfully. After inserting the document as object i have to mention the file name for the object. Is there any way to mention the Object name using code?. >>

    Are you talking about the manual insertion shown near the very beginning of the article, or about the Open XML code? What do you mean with "the Object name"?

    <<And if i try to insert more than one object (different files) in document it shows the second object as image. How to insert more than one object in word document?.>>

    We'd need to see your code, but are you remembering to give each newly embedded object its own rId number:

    var embeddedPackagePart =
          mainDocumentPart.AddNewPart<EmbeddedPackagePart>(
          "application/vnd.openxmlformats-" +
          "officedocument.wordprocessingml.document",
          "rId1");


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, April 7, 2013 6:04 AM
    Moderator
  • Hi Cindy,

    Thanks for the reply.

    <<And if i try to insert more than one object (different files) in document it shows the second object as image. How to insert more than one object in word document?.>>

    Yes I am giving new rld number for each embedded object. And I have solved this issue. Problem was, same Shape ID given for all object i was inserted.

    new EmbeddedObject( new v.Shape( new v.ImageData() { Title = "", RelationshipId = imageId } ) { Id ="_x0000_i1025",

    // This id has to be changed for each object we are going to insert. Style = "width:76.5pt;height:49.5pt", Ole = false } new ovml.OleObject() { Type = Ovml.OleValues.Embed, ProgId = "Word.Document.12", ShapeId = "_x0000_i1025", // Id should be same which we are using for shape. DrawAspect = ovml.OleDrawAspectValues.Icon, ObjectId = "_1299573545", Id = embedId } )



    << <<i am able to embed documents in word successfully. After inserting the document as object i have to mention the file name for the object. Is there any way to mention the Object name using code?. >>

    Are you talking about the manual insertion shown near the very beginning of the article, or about the Open XML code? What do you mean with "the Object name"? >>

    I am talking about the Open XML code. Now i am able to insert objects (word file) as many as we want, but by default it shows the caption of the object as "Embedded document". We need to change this caption for each object (word file) using code. Please suggest a way to do this.

    Here is the sample code which we are using to embed doc file as object using Open XML.

    var embeddedPackagePart = mainDocumentPart.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ObjectID"); GenerateEmbeddedPackagePart(embeddedPackagePart, embeddedDocumentPath);

    // Function used to generate embedded object. Same as give in "http://msdn.microsoft.com/en-ca/library/ee355229.aspx" var imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/x-emf", "ImageID"); GenerateImagePart(imagePart);

    //Function used to generate image file using Base64String. Same as give in "http://msdn.microsoft.com/en-ca/library/ee355229.aspx" Paragraph p = GenerateMainDocumentPart("ImageID", "ObjectID", "_x0000_i1025"); var res = from BookMarks in mainDocumentPart.Document.Body.Descendants<BookmarkStart>() where BookMarks.Name == "BookMarkName" select BookMarks; var bookmark = res.SingleOrDefault(); if (bookmark != null) { OpenXmlElement BookMarkParent = bookmark.Parent; BookMarkParent.InsertAfterSelf(p); bookmark.Remove(); }

    private static Paragraph GenerateMainDocumentPart(string imageId, string embedId,string shapeid) { var element = new Paragraph( new Run( new EmbeddedObject( new v.Shape( new v.ImageData() { Title = "", RelationshipId = imageId } ) { Id =shapeid, Style = "width:76.5pt;height:49.5pt", Ole = false }, new ovml.OleObject() { Type = Ovml.OleValues.Embed, ProgId = "Word.Document.12", ShapeId =shapeid, DrawAspect = ovml.OleDrawAspectValues.Icon, ObjectId = "_1299573545", Id = embedId } ) { DxaOriginal = (String)(UInt32Value)1531UL, DyaOriginal = (String)(UInt32Value)991UL } ) ); return element; }



    Monday, April 8, 2013 5:42 AM
  • Hi Joseph

    OK, now I understand your question.

    Looking at the internal XML, I see that
     1. The document is embedded as a binary OLE object, even though it's an Open XML file. This is because "embedding" is an OLE thing, and OLE is binary. 
      2. The caption is not stored anywhere in the file "in the clear". Word must be storing it in that binary information.
      3. When you double-click the embedded image it opens as "Document in [DocumentName]": Word isn't using the caption to label the document when it's opened to be read/edited.

    Theoretically, this would be Word's old binary file format. The specs for this are available to the public; you'll find a link to that information as well as a forum that deals with it here: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/063a23a6-1595-4c83-a25f-6c94658c4649. If you're lucky, you could extract/edit this in the binary...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 8, 2013 7:14 AM
    Moderator
  • Thanks Cindy, 

    I am trying to edit the binary data related to image part in embedded object.  Hope the issue will be solved in this approach.

    Thanks,

    Joseph Regis

    Tuesday, April 9, 2013 9:45 AM
  • Hi Cindy,

    Is there any way to insert file as object in word document using Open XML other than combining image and OLE object as Embedded object?

    Thanks,

    Joseph Regis

    Tuesday, April 9, 2013 10:54 AM
  • Hi,

    With the help of following link "http://msdn.microsoft.com/en-ca/library/ee355229.aspx" i am able to embed documents in word successfully. After inserting the document as object i have to mention the file name for the object. Is there any way to mention the Object name using code?. 

    When I tried to insert document as object, it shows the document as embedded object with the name as "Embedded Document". After verifying the code, the object name comes from imagepart which is used to generate OLE object. Is there any way to edit this embedded object to change the name after inserting?

    Thanks in advance :)
    Tuesday, April 23, 2013 5:06 AM
  • Hi Joseph

    Look at the following code recorded for inserting a document as an OLE object icon. It shows you the property name for the icon label. Note, therefore, that you can specify this when the object is created using the Word API.

    Selection.InlineShapes.AddOLEObject(ClassType:="Word.Document.12", _
            fileName:="C:\Users\Cindy Meister\Documents\DocVarTest.docx", LinkToFile _
            :=False, DisplayAsIcon:=True, IconFileName:= _
            "C:\PROGRA~1\MICROS~2\Office14\WINWORD.EXE", IconIndex:=1, IconLabel:= _
            "DocVarTest.docx")

    The following bit of code demonstrates how you can read/write this information via the Word APIs

    Sub GetOleIconLabel()
        Dim oleDoc As OLEFormat
        
        Set oleDoc = ActiveDocument.InlineShapes(1).OLEFormat
        Debug.Print oleDoc.IconLabel
        oleDoc.IconLabel = "Testing..."
    End Sub
    


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, April 23, 2013 6:55 AM
    Moderator
  • Hi Cindy,

    Thanks for the reply. 

    Sorry I forget to mention I am using OpenXML to do this. Since Word API requires Word has to be installed in server where I m hosting, I used OpenXML sdk (to avoid installation). Is there a way edit oleDoc.IconLabel using OpenXML?

    Thanks,

    Joseph Regis

    Tuesday, April 23, 2013 7:36 AM
  • Hi Joseph

    1) You've posted this time in the forum for the Word API. Of course, that's the kind of answer you're going to get.

    2) We've been over this topic twice in the Open XML SDK forum and my answer there still stands. The code you see that does affect the caption supports the answer I gave you there: this is stored in the binary OLE. The only way you can access it would be to find out how to read the binary of the embedded file. For information on that you need to ask in the forum that supports the old binary file format.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, April 23, 2013 8:08 AM
    Moderator
  • Hello,

    You have posted a very similar question before: http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/cac82238-6487-4198-be48-37513bfd4e6c

    And there has been some replies to your questions. Please follow it to ask for solutions. And we will merge this thread to the previous one. Thanks for your understanding.

    Regards,


    Damon Zheng
    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.

    Wednesday, April 24, 2013 8:22 AM
    Moderator
  • Hi joseph,

    Hope by now you would have resolved this, just posting this reply for others.

    While embedding multiple objects in open XML doc we need to ensure "ObjectId" and all OVML part "Id"s and ShapeId to be unique.

    I did face the same issue while working on one POC. I had to embed document in table, and one document to each row. I did append the row number to each id to make them unique across deck. 

    Friday, March 21, 2014 7:02 PM
  • Can you please send me the complete code as i am having similar kind of requirement where i need to add html file as oleobject to word.

    Even me i can't use Microsoft dll's as it needs ms office to be installed on the server.

    Monday, June 22, 2015 8:58 AM
  • Can you please send me the complete code as i am having similar kind of requirement where i need to add html file as oleobject to word.

    Even me i can't use Microsoft dll's as it needs ms office to be installed on the server.

    Monday, June 22, 2015 8:59 AM
  • Can you please send me the complete code as i am having similar kind of requirement where i need to add html file as oleobject to word.

    Even me i can't use Microsoft dll's as it needs ms office to be installed on the server.

    Monday, June 22, 2015 8:59 AM
  • Hi,

    I didn't got exact solution for this. So as a work around, i have place the file name just below the embedded object as another tag.

    Tuesday, June 23, 2015 8:02 AM