views:

27

answers:

1

I have this piece of c# code in my Excel 2003 add-in:

var leafPoint = m_worksheet.Shapes.Item("aPoint").Duplicate();

leafPoint.Name = "Shape" + (m_shapesNameIndex++).ToString();
leafPoint.OnAction = m_worksheet.CodeName + ".PointClicked";
leafPoint.AlternativeText = 

string.Format("Correlation Value: {0}",     
    item.PointData.Correlation.ToString("0.0000;-0.0000"));

leafPoint.Top = item.LeftChildNode.Top + 
    ((item.RightChildNode.Top - item.LeftChildNode.Top) / 2) + 
    (leafPoint.Height / 2);

When I run it in Excel 2003, it works perfectly. However, when I run it in Excel 2007, the shape's Top value is off... It always ends up a few pixels above the intended location!

When I looked at the logs, in Excel 2003 the shapes were consistently placed in the correct position, but in Excel 2007, when the code attempts to place the shape's Top position, Excel 2007 seems to be overriding the value for some reason (I think).

For instance, in one case the leafPoint.Top value resolved to 206.25. In Excel 2003, that was indeed the result. However, in Excel 2007, this value ends up becoming 204.2954...

Does anyone have some insight into this issue?

A: 

I finally figured it out. Based on this link Excel 2007 Service Pack 2 – the official fix list, it mentions among the fixed bugs:

The value of the Top property of the Line object is affected by Zoom level. This leads to incorrect placement of line shapes when this value is set programmatically to lines on a sheet that is zoomed to anything other than 100%.

I figured if the Line object was affected, possibly could there have been a problem with the Shape object as well?

So I checked my template, and indeed, it was set to 80%. (Line objects work fine, by the way, since my Excel version has SP2 installed). As soon as I set the zoom level to 100%, the shape objects suddenly rendered to the position they were expected to.

Another note: this problem only occurs during rendering. Once all the shapes have been drawn, I can set the zoom level to whatever I want, and the shapes position themselves properly.

Conclusion: whenever drawing shapes in Excel, always set the zoom level to 100%. After all the drawing is done, you can set the zoom level back to the original desired zoom.

code4life