Embedded Excel - image scaling issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have an embedded excel sheet in which I need to refresh the image for (via
C# and Interop - dont ask). I am finding if I select the shape, activate the
Excel and then unselect the active selection, I end up with a skewed image
for the excel:

_pptApp.ActiveWindow.View.GotoSlide(s.SlideIndex);
//select the shape
shape.Select(MsoTriState.msoFalse);
//activate the embedded sheet - refreshes the image for excel
shape.OLEFormat.Activate();
//deselect the excel and image is updated
_pptApp.ActiveWindow.Selection.Unselect();

I am wondering how I can fix this. I find if I open the powerpoint
afterwards manually, activate the Excel, slightly scale it, and then
deactivate, I get an image with no skewing. I dont know how to do this via
code though.
 
While I don't code in C#, I do in VBA. Here's how I would do it in VBA

Declare 4 variables for .top, .left, .width and .height.
Get those values when you select the shape before activating Excel.
Make your changes like you are doing.
Then reset the shape's dimensions before you unselect it.

Brian Reilly, MVP
 
Brian,

I should elaborate a bit more. I have a known workable area. When I
refresh the image, the new one will be completely different dimensions, so I
scale it back to fit the workable area using ScaleWidth. The refreshed
image, though, is skewed. See below:

_pptApp.ActiveWindow.View.GotoSlide(s.SlideIndex);
//select the shape
shape.Select(MsoTriState.msoFalse);
//activate the embedded sheet - refreshes the image for excel
shape.OLEFormat.Activate();
//deselect the excel and image is updated

_pptApp.ActiveWindow.Selection.Unselect();

//THE PROBLEM LIES HERE - the resulting image of the excel table is skewed

//now need to scale excel shape to area
float scale = 1;
if (shape.Height > shape.Width) //what is the deciding factor
{
if (shapeHeight > shapeWidth)
{
scale = shapeWidth / shape.Width;
}
else
{
scale = shapeHeight / shape.Height;
}
}
else
{
if (shapeHeight < shapeWidth)
{
scale = shapeWidth / shape.Width;
}
else
{
scale = shapeHeight / shape.Height;
}
}
shape.ScaleWidth(scale, MsoTriState.msoFalse,
MsoScaleFrom.msoScaleFromMiddle);

Essentially I can do in C# whatever can be done in VBA. From my initial
post, I indicated if I manually (by mouse) expanded the activated excel
workbook a little, and then unselected it, the refreshed image has no
skewness.

So is there a way to do this via VBA or some other way to achieve the same
thing?
 
comet61,
I'm not sure I follow you. Are you changing/expanding the
sourcedatarange for the chart in Excel? Or try making sure your code
has Keep Aspect Ratio and Relative to original picture checked (Under
Format + Object + Size).
Or try, activating excel and doing what you have to do. Then copy the
chart to the clipboard. Go back to PPT delete the old chart and paste
the new one in. (Although that option is not well tested).

Perhaps Jon Peltier, an Excel chart expert who also uses PPT will pop
in. His first suggestion would probably be to paste as picture (rofl
Jon) but then you'd have no data embedded in the PPT file.

Please explain more of exactly what you are doing.

Brian Reilly, MVP
 
Back
Top