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: C#, Shapes, Shared Addin
