How to insert OLE Excel document without presentation image using .NET

Hi,

I’ve planned to use your Aspose.Word solution to build some word documents but i have a few questions to ask you before.

I’ve seen in your API that it was possible to insert embedded object using builder.InsertOleObject. I’ve tried this :

builder.InsertOleObject(_workbookPath + "VolumeExecution.xlsm", "Excel.Sheet.12", false, false, null);

This code generate the attached Capture1.JPG (3.9 KB)
preview. But I need to have an excel preview and not an image preview (Capture2.JPG (12.7 KB)
).

Is there a way to get what I need?

Thanks,

@lswe,

Thanks for your inquiry. Aspose.Cells supports converting Excel worksheets to images. A simple solution would be to just convert Worksheet to image and then use that preview image in DocumentBuilder.InsertOleObject method as follows. Hope this helps you.

DocumentBuilder builder = new DocumentBuilder();
builder.InsertOleObject(MyDir + "VolumeExecution.xlsm", "Excel.Sheet.12", false, false, Image.FromFile(MyDir + "SheetImage.jpg"));

In fact, adding a worksheet in a document using Word allows to not include any preview image (Insert => Object => Microsoft Excel Worksheet). Your API does not allow this, isn’t it ?

I have another question about this. My needs are quite easy :

Consider I have a document with some bookmarks inside. Each bookmark contains an OLE object (Excel sheet). I need to replace all the OLE object with another OLE object but all the style properties of the old OLE object have to be applied to the new one.

Is there a way to do this?

Thanks,

@lswe,

Thanks for your inquiry.

Unfortunately, Aspose.Words does not support the requested feature at the moment. However, we have logged this feature request as WORDSNET-15754 in our issue tracking system. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

Please move the cursor to the bookmark and remove the OLE object. The node type of OLE is Shape. After removing the OLE, please insert the new OLE object using DocumentBuilder.InsertOleObject. If you face any issue, please share your input and expected output documents here for our reference. We will then provide you more information on this.

Thanks for your answer,

I made 2 documents (Input.zip (19.3 KB) & Output.zip (21.0 KB)) which explains and shows what I need to do with Aspose.Cells.

Could you please provide me a detailled algorithm that do the job?

Thanks,

@lswe,

Thanks for sharing the documents. Please use following code example to replace the existing OLE with new one. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
                
builder.MoveToBookmark("Bookmark1");
Shape shape = (Shape)builder.CurrentParagraph.GetChild(NodeType.Shape, 0, true);
if (shape != null)
{
    shape.Remove();
    builder.InsertOleObject(MyDir + "VolumeExecution.xlsm", "Excel.SheetMacroEnabled.12", false, false, Image.FromFile(MyDir + "SheetImage.png"));
}
                 
doc.Save(MyDir + "output.docx");

@lswe,

Thanks for your patience. It is to update you that we have closed the issue (WORDSNET-15754) with ‘‘Won’t Fix’’ resolution.

Please note that when user inserts Excel file into Word application (regardless Word GUI or Word VBA), Word calls Windows API to know application registered in Windows registry for .xls documents. Then Word makes several COM (Component Object Model) calls to Excel application, inviting it to embed document and draw presentation image. Excel application replies for invitation and saves document data and renders presentation image using COM calls again. In short, presentation image is rendered by other application.

Since Aspose.Words is cross-platform application and might be running on, for example, Linux server we cannot call for Windows registry, COM, etc, on which OLE embedding is based. The solution would be to convert Worksheet to image using Aspose.Cells and then use that preview image in DocumentBuilder.InsertOleObject method.

Thanks for your answer,

However I don’t think that this code will apply the old OLE object style (table height/width) to the new one, isn’t it ?

The solution to convert an Excel worksheet as a preview image is interesting but the image won’t be updated if the Excel sheet is updated by the user so this preview may be obsolete.

@lswe,

Thanks for your inquiry. When you update the OLE (Excel worksheet) using MS Word, its preview is updated according to OLE. Please generate a Word document using Aspose.Words and then update the OLE using MS Word to check this behavior.

Yes you are right Word automatically update the image. But you did not answer to my first question :

However I don’t think that this code will apply the old OLE object style (table height/width) to the new one, isn’t it ?

@lswe,

The DocumentBuilder.InsertOleObject method inserts the embedded or linked OLE object. This method does not effect the other OLE embedded in document. Could you please share some more detail about your query? We will then provide you more information on this.

As I said on my previous post, my aim is to replace an OLE object inside a bookmark by another one.The code you provide me do the job. But MS Word provides the feature to resize the OLE object as well as it may format it. Let’s consider a simple document with a bookmark and an OLE object inside. When I open this document on MS Word, I can resize it to fit in the full page width and add a border around it for example. When I’m executing your sample code, the new OLE object that replace the old one with its style has no border and does not fil to the full page width. It is added as a new OLE object without any format style.

Is there a way to copy the deleted OLE object and to apply it to the new one ?

@lswe,

Thanks for sharing the detail. The Shape class represents an object in the drawing layer, such as an AutoShape, textbox, freeform, OLE object, ActiveX control, or picture.

You can use Shape.Width and Shape.Height properties to change the size of OLE. Moreover, please use Shape.Stroke property to set the stroke of shape.

You can get the properties of OLE before replacing it with new one. Please check the properties of Shape class.

Hi,

Now I am able to be more specific about my needs. In fact, my input document is a Word which contains a couple of ContentControl. What I need to do is to replace (or insert inside) these content controls with some Excel range.

The key part of my problem is to insert only Excel range because I am only able to insert the full workbook using OLE object.

For instance, let’s consider that I have on one hand a Word document which contains only one Content control and on the other hand an Excel workbook, which contains 2 worksheets with a A1:L15 table.

Let’s suppose that I need to insert in the Word document content control, the A1:B2 cells of the first worksheet.

Is there a way to do that with Aspose.Words without using LINK ? Because if I use Link, my document won’t be independant, as it will be built on server side, I would have to return all the linked workbook with my document… and it’s not a satisfying option…

Thanks for your answer,

@lswe,

Thanks for your inquiry. Please ZIP and attach your input, expected output and Excel documents here for our reference. We will then provide you more information about your query. Thanks for your cooperation.

Thanks for your answer,

You will find here (41.1 KB) a zip that contains :

  • a workbook
  • an input file
  • an output file

As you will see, the output file only display the A1:C3 range of the workbook inside the content control of the input file.

Thanks for your help,

Regards,

@lswe,

Thanks for your inquiry. You can achieve your requirement using Aspose.Words and Aspose.Cells. In your case, we suggest you following solution.

  1. Export Range of Cells into stream.
  1. Move the cursor to the first node of content control.
  2. Use DocumentBuilder.InsertOleObject to insert the OLE object from a stream into the document.

Please refer to the following article about saving workbook to stream.
Saving File to a Stream

A post was split to a new topic: Issue with Image presentation vector based emf files/streams