OLE Excel Object Size is Changed after Double Click on it in MS Word | Insert OLE (Excel) into Document using .NET

@RadAway

Using the latest version of Aspose.Cells and Aspose.Words, we have not found the shared issue. So, please upgrade to the latest versions of Aspose.Wrods and Aspose.Cells.

I am attaching a video demonstrating the issue. problem.7z (556.2 KB)

I set the zoom level to 40%:

sheet.Zoom = 40;

Notice around second 12-13 when the embedded Excel file is opened it is briefly correctly 40% zoomed and then it resets to 100%.

What I want to accomplish is to have the Excel OLE that I am embedding into the Word document using Apsose to be opened at the appropriate zoom level so that the image of the OLE does not get resized after the initial opening.

If you manually resize the OLE image, the embedded Excel will subsequently open at the appropriate zoom level so that the OLE image does not have it’s size changed. So there must be a way to specify the zoom level in the OOXML?

@RadAway

Could you please share the modified code example that you are using? We will investigate the issue and provide you more information on it.

Sure. Below is the code.
You can set the zoom level to different values and note that on initial Excel OLE open it flashes to the expected zoom level and then resets to 100%.

static void TestAspose()
{
    var book = new Aspose.Cells.Workbook(@"C:\Projects\WordTest\sample.xlsx");

    var sheet = book.Worksheets[0];

    book.Worksheets.SetOleSize(0, sheet.Cells.MaxDataRow, 0, sheet.Cells.MaxDataColumn);

    sheet.PageSetup.LeftMargin = 0;
    sheet.PageSetup.RightMargin = 0;
    sheet.PageSetup.TopMargin = 0;
    sheet.PageSetup.BottomMargin = 0;
    sheet.Zoom = 40;

    var options = new Aspose.Cells.Rendering.ImageOrPrintOptions
    {
        OnePagePerSheet = true,
        ImageType = Aspose.Cells.Drawing.ImageType.Emf
    };

    var sr = new Aspose.Cells.Rendering.SheetRender(sheet, options);

    var imgStream = new MemoryStream();
    sr.ToImage(0, imgStream);
    imgStream.Position = 0;

    var document = new Aspose.Words.Document();
    var builder = new Aspose.Words.DocumentBuilder(document);
    using var xlsStream = new MemoryStream();

    book.Save(xlsStream, SaveFormat.Xlsx);
    xlsStream.Position = 0;
    var shape = builder.InsertOleObject(xlsStream, "Excel.Sheet.12", false, imgStream);

    var of = shape.OleFormat;
    of.AutoUpdate = false;


    shape.AspectRatioLocked = true;
    shape.Width = 600;

    document.Save(@"C:\Projects\WordTest\sample.docx");
}

@RadAway

If you insert the same Excel OLE into Word document using MS Word, you will face the same behavior. So, this is not an issue with Aspose.Words.

We are moving this forum thread to Aspose.Total forum where Aspose.Cells team provides you information regarding Aspose.Cells if there is any solution available for your case.

But after you manually size the OLE image, it WILL maintain the correct zoom level on subsequent opens of Excel.

@RadAway,

How could you accomplish the task manually without using or involving Aspose APIs? I mean sheet’s zoom level (what you have set in Excel manually) prevails over when clicking the embedded OLE object. Please give us steps details and give expected file(s) with screenshots. I suspect this might be MS Office (application) behavior regarding embedded OLE objects and nothing to do with Aspose.Cells part.

Attaching a video demonstrating the behavior.
demo.7z (1.6 MB)

The first time I open the OLE on a document generated by Aspose it resets to 100% zoom (10 seconds). I then resize the image of the OLE (21 seconds) and open the OLE again, and it is now shown with the appropriate zoom level (31 seconds).

To reiterate my point: after I manually adjust the image size at 21 seconds, a specific zoom level is applied when I open the embedded Excel. This means that the zoom level or some parameter gets set either in the OLE properties in Word or on the embedded Excel spreadsheet.

@RadAway,

Well, you are manually setting or resizing the embedded OLE object (in Word document) after sometime. I asked you to show the your expected file when clicking the OLE object first time where the OLE object size should prevail or retain.

book.Save(xlsStream, SaveFormat.Xlsx);

Please note, this is the line where you just save the final excel file to streams after doing all the OLE object’s and PageSetup settings. Now, Aspose.Cells role is over in the task at that time and you navigate to Aspose.Words API next. But as you manually adjust the size after clicking over it afterwards, so, not sure if Aspose.Words has anything to do with it. In short, this might be the behavior of MS Office (e.g. MS Word). Anyways, we will comment and give further details from Aspose.Words API perspective (if there is something to do with it).

I am not saying that I expect Aspose to have any control of what Word or Excel do when a user starts manually adjusting things.

The point I was trying to make in the previous post is that when a user manually resizes the OLE image, a parameter gets set somewhere which controls the zoom level of the Excel spreadsheet when it is subsequently opened. This means that such a parameter potentially exists in the OOXML definition of the Word document or Excel spreadsheet. Do you agree with this assessment?
If so, then maybe the Aspose API could be enhanced to allow the setting of this parameter so that it can be set to something (other than the default 100%) when Aspose generates the document (or spreadsheet).

@RadAway

Please manually create the expected Excel document using MS Excel that you want to insert into MS Word document. Please insert this document into Word document using MS Word and check the MS Word’s behavior. You will notice the same behavior.

If you notice different behavior, please share following resources here for testing.

  • Your expected Excel document that you want to insert into Word document. Please create this document using MS Excel.
  • Please attach the output Excel document that is generated by Aspose.Cells and shows undesired behavior and .
  • Please share Aspose.Cells code example to generate Excel document.

We will then investigate the issue and provide you more information on it. If Aspose.Cells does not generate the expected Excel output, we will log this issue for Aspose.Cells.

PS: To attach these resources, please zip and upload them.

You’ve completely ignored what I wrote in my post. Are you not understanding the point I am making or is what I stated somehow incorrect?

@RadAway

We have investigated this issue further and noticed that the OLE Shape width does not set correctly. The Shape.Width should be equal to size set by SetOleSize method.

The Shape.Width does not change the width of OLE as MS Word does. We have logged this detail in our issue tracking system and you will be informed once there is any update available on it.

@RadAway

Further to my previous post, you can set the OLE width using following code snippet. The Shape width should be the size of OLE set by SetOleSize.

Bitmap bitmap = sr.ToImage(0);
shape.Width = bitmap.Width;

Hope this helps you.

Ok that makes sense. So you are acknowledging that shape.Width not taking effect is a bug and will be fixed at some point?

@RadAway

This code will solve your problem. However, as shared in my previous post, the Shape.Width sets the shape size but not the internal OLE size.

It is more related to missing feature in Aspose.Words. We will check the possibility of implementation of this feature and inform you via this forum thread.

Well it doesn’t actually solve my problem because I need to resize the image to get it to fit properly into the document, but I am glad to hear that you will be looking into it. Here is updated code:

static void TestAspose()
{
    var book = new Aspose.Cells.Workbook(@"C:\Projects\WordTest\sample.xlsx");

    var sheet = book.Worksheets[0];

    book.Worksheets.SetOleSize(0, sheet.Cells.MaxDataRow, 0, sheet.Cells.MaxDataColumn);

    sheet.PageSetup.LeftMargin = 0;
    sheet.PageSetup.RightMargin = 0;
    sheet.PageSetup.TopMargin = 0;
    sheet.PageSetup.BottomMargin = 0;

    var options = new Aspose.Cells.Rendering.ImageOrPrintOptions
    {
        OnePagePerSheet = true,
        ImageType = Aspose.Cells.Drawing.ImageType.Emf
    };

    var sr = new Aspose.Cells.Rendering.SheetRender(sheet, options);

    var imgStream = new MemoryStream();
    sr.ToImage(0, imgStream);
    imgStream.Position = 0;

    using var image = Aspose.Imaging.Image.Load(imgStream);
    image.ResizeWidthProportionally(600);
    image.Save();
    imgStream.Position = 0;

    var document = new Aspose.Words.Document();
    var builder = new Aspose.Words.DocumentBuilder(document);
    using var xlsStream = new MemoryStream();

    book.Save(xlsStream, SaveFormat.Xlsx);
    xlsStream.Position = 0;
    var shape = builder.InsertOleObject(xlsStream, "Excel.Sheet.12", false, imgStream);

    var of = shape.OleFormat;
    of.AutoUpdate = false;

    shape.AspectRatioLocked = true;
    shape.Width = image.Width;

    document.Save(@"C:\Projects\WordTest\sample.docx");
}

@RadAway

We will inform you via this forum thread once there is any news available on WORDSNET-22615.

Are there any updates on this? Is it still being looked at?

@RadAway

Unfortunately, there is no update available on your issue. We will be sure to inform you via this forum thread once this issue is resolved. We apologize for your inconvenience.