In a VSTO (C#) project I want to take the UsedRange including the shape objects like pictures and charts.
1. Is there a way to get the UsedRange with shape objects?
Since I couldn’t find such a method, I thought of calculating the edges by using this:
foreach (Excel.Shape shape in shapeObjects)
{
float shapeBottom = shape.Top + shape.Height;
float shapeRight = shape.Left + shape.Width;
if (maxBottom < shapeBottom)
{
maxBottom = shapeBottom;
}
if (maxRight < shapeRight)
{
maxRight = shapeRight;
}
}
Excel.Range r = <XXX>(maxBottom, maxRight);
So now I have the problem of getting the Range from maxBottom and maxRight.
2. Is there a way to get a Range object by giving the width and height?
Shapes have a BottomRightCell property which returns the cell covered by the lower right corner of shape.
If you iterate through that property for each shape you should be able to collect the right-most column and the last row for the range you want.