I have a spreadsheet that is created programatically with a lot of comments in it(up to 40,000). The comments get resized after deleting several columns from the worksheet. This is apparently a bug in excel. ( http://answers.microsoft.com/en-us/office/forum/office_2007-excel/excel-comment-boxes-resizing-themselves-andor/3fdf3e72-6ca5-4186-a656-b7b6fd8db781?msgId=d55534a5-4603-482e-ac97-9ec260124f78 )
Ideally I would like to AutoSize all the comments at once after deleting the columns.
Attempting to avoid looping through each individual comment, here's what I've tried so far.
- Setting AutoShapeDefaults has no effect - the comments still get resized after deleting the columns.
- XlPlacement property. XlMove and XLMoveAndSize have no effect.
- Worksheet.Shapes.SelectAll throws an OutOfMemory Exception no matter the amount of comments
My thought is to get a ShapeRange object of all the comments in the spreadsheet and set the size from there.
This works perfectly:
public static void ResizeComments()
{
Microsoft.Office.Interop.Excel.Workbook objWorkbook;
objWorkbook = (Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
Worksheet objSheet = (Worksheet)objWorkbook.ActiveSheet;
int[] test = {1,2,3,4,5};
ShapeRange sRange = objSheet.Shapes.Range[test];
sRange.Height = 100;
sRange.Width = 220;
}
Changing to this throws an exception "Exception from HRESULT: 0x800A03EC" at the AutoSize line.
ShapeRange sRange = objSheet.Shapes.Range[test];
sRange.TextFrame.AutoSize = true;
Using my actual array of Shape Indices throws the same exception but at Shapes.Range[]. I've looked at the shapes variable while debugging and it's identical to test except it's int[249] instead of int[5];
int[] shapes = (int[])shapes.ToArray(typeof(int));
ShapeRange sRange = objSheet.Shapes.Range[shapes];
int[] shapes = (int[])shapes.ToArray(typeof(int));
Because I attempted to replicate your method in VBA and could successfully generate a ShapeRange of 50000 comments at once. However, if I attempt to select 50001 (with only 50000 shapes) it fires the same error as using AutoSize. Make sure the array is valid. – Anciently