Multiple OleObjects (Linked Objects) are not getting merged properly using DocumentBuilder

Apr 12, 2013 at 8:42 AM

I'm getting issues during merging multiple documents having OleObjects (Linked Objects) having external relationships to files using DocumentBuilder.

You can reproduce this issue by having multiple linked objects in it and try to merge them using DocumentBuilder.

As a result you will have a merged document with OleObjects with broken external relationship and you will no longer able to update those objects by clicking them.

I just want to know if this is the issue with DocumentBuilder or I am doing something wrong?

Apr 12, 2013 at 10:51 AM
Hi Fenil,

That is an issue with DocumentBuilder. Currently it does not support merging documents that reference OleObjects. It is a great idea for a future enhancement.

Regards, Eric
Apr 12, 2013 at 2:16 PM
Edited Apr 12, 2013 at 2:16 PM
Thanks Eric for the response.

I guess DocumentBuilder supporting OleObjects and I've successfully able to merge OleObjects(Linked Objects, e.g, Excel file) if they are in only one source document.

But as soon as multiple documents have OleObjects, after merging they links of OleObjects are getting broken apart from the first OleObject in the document. If possible I could share demo application for this issue.

Apr 12, 2013 at 6:03 PM
I haven't done the analysis of what would be required to support OleObjects. There probably should be some check to make sure that the same OleObject isn't duplicated in the destination document. Then to test a number of scenarios, making sure all bases are covered.

It will be some time before I can get to this. I am over-booked for the next 2 1/2 months.

I do have PowerTools on my agenda, and that includes some important enhancements to DocumentBuilder.

Cheers, Eric
Apr 12, 2013 at 6:38 PM
Thanks again for your quick response.

Today, After investigating further I found that during merging, OleObjects getting duplicated with same id with duplicated shape and shapetypes as well. Probably that could be an issue.

I'll look into it further and will try to solve it if possible.

If you can provide any suggestions it will be helpful.

Thanks again for your time.

Apr 12, 2013 at 8:46 PM
In DocumentBuilder, you will find a class that it uses to prevent duplication of images. The correct approach is to make a variation of that class that prevents duplication of the OleObject. The code will be almost identical. It could be possible to modify the class to prevent image duplication so that it prevents duplication of multiple types of things, i.e. images and OleObjects, but it is a small class and it is probably more effort that it is worth. I would probably just create a new class that is nearly identical to that one.

Cheers, Eric
Apr 24, 2013 at 1:02 PM
Hi Eric,

I've just resolve the issue by making the unique ids for all the shape, shapeTypes and objects associates with each other. And this simple solution have resolve my issue. Below is the code, Let me know if anything is unusual.
 private static void FixUpEmbeddedObjectIds(WordprocessingDocument outputWPDocument)
     foreach (EmbeddedObject obj in outputWPDocument.MainDocumentPart.Document.Descendants<EmbeddedObject>())
         var shapeType = obj.Descendants<DocumentFormat.OpenXml.Vml.Shapetype>().FirstOrDefault();
         var shape = obj.Descendants<DocumentFormat.OpenXml.Vml.Shape>().FirstOrDefault();
         var oleObject = obj.Descendants<OleObject>().FirstOrDefault();

         if (shapeType != null && shape != null)
             shapeType.Id = Guid.NewGuid().ToString();
             shape.Type = shapeType.Id;
         if (shape != null && oleObject != null)
             shape.Id = Guid.NewGuid().ToString();
             oleObject.ShapeId = shape.Id;