Wednesday, June 21, 2006

Working with Excel Shapes from C#

In VBA, you can select shapes and control them usingSelection.ShapeRange. Selection morphs to different types depending on what property is called. Go here for more information.

In C#, the Selection(range) object does not have a ShapeRange property. However the DrawingObjects object does have aShapeRange property. So if you want to copy a ShapeRange from one sheet to another, try the following.

Say you have this VBA code to port to C#:

Set aShape = srcSheet.DrawingObjects.ShapeRange.Group
x = aShape.Left
y = aShape.Top
aShape.Copy
dstSheet.Paste
Selection.Left = x
Selection.Top = y
Selection.ShapeRange.Ungroup
aShape.Ungroup


This converts to:

aShape = (Excel.Shape)drawObj.ShapeRange.Group();
x = aShape.Left;
y = aShape.Top;
aShape.Copy();
dstSheet.Paste(Missing, Missing);
drawObj = (Excel.DrawingObjects)dstSheet.DrawingObjects(Missing);
Excel.ShapeRange shapeRng = (Excel.ShapeRange)drawObj.ShapeRange;
shapeRng.Left = (float)x;
shapeRng.Top = (float)y;
shapeRng.Ungroup();
aShape.Ungroup();

This solution assumes that there was nothing on the destination sheet to begin with. (the line dstSheet.DrawingObjects refers to all the Drawing objects on the sheet)

Labels: , ,

Friday, June 02, 2006

Using RibbonX UI with Office 2007 from C# COM Addin

Hi all,

Yes, I am here still. I have been trying to use the RibbonX control in Office 2007 from a C# COM Addin. There is only one other blog that I have seen that has touched on the subject so far http://pschmid.net , so I look forward to seeing what he has to say in the coming weeks.

Any other blogs out there covering this subject?

Labels: , ,