使用ShapeRange在Excel中调整注释大小

use*_*668 6 c# excel comexception

我有一个以编程方式创建的电子表格,其中包含大量注释(最多40,000个).从工作表中删除多个列后,注释会调整大小.这显然是excel中的一个错误.(http://answers.microsoft.com/en-us/office/forum/office_2007-excel/excel-comment-boxes-resizing-theselfself-andor/3fdf3e72-6ca5-4186-a656-b7b6fd8db781?msgId=d55534a5-4603 -482e-ac97-9ec260124f78)

理想情况下,我想在删除列后立即自动调整所有注释.

试图避免循环遍历每个评论,这是我到目前为止所尝试的内容.

  • 设置AutoShapeDefaults无效 - 删除列后,注释仍会调整大小.
  • XlPlacement属性.XlMove和XLMoveAndSize无效.
  • 无论评论量多少,Worksheet.Shapes.SelectAll都会抛出OutOfMemory异常

我的想法是获取电子表格中所有注释的ShapeRange对象,并从那里设置大小.

这非常有效:

        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;
        }
Run Code Online (Sandbox Code Playgroud)

更改为此会在AutoSize行引发异常"来自HRESULT的异常:0x800A03EC".

        ShapeRange sRange = objSheet.Shapes.Range[test];
        sRange.TextFrame.AutoSize = true;
Run Code Online (Sandbox Code Playgroud)

使用我实际的Shape Indices数组会抛出相同的异常,但是在Shapes.Range []中.我在调试时查看了形状变量,除了它是int [249]而不是int [5]之外,它与test相同.

        int[] shapes = (int[])shapes.ToArray(typeof(int)); 
        ShapeRange sRange = objSheet.Shapes.Range[shapes];
Run Code Online (Sandbox Code Playgroud)

Apo*_*s55 1

好吧,我将用必须从 Excel 模块内运行的 VBA 代码来回答。来自这里的讨论和回答。

Sub CommentFixer()
Dim Arng As Range, Acl As Variant, InitRng As Variant, MaxSize
Set InitRng = Selection
Set Arng = Application.InputBox("Select Ranges", , , , , , , 8)

For Each Acl In Arng
    If (Not (Acl.Comment Is Nothing)) And (Acl.MergeArea.Count = 1) Then
        Acl.Select
        Selection.Comment.Visible = True
        Selection.Comment.Shape.TextFrame.AutoSize = True
        'Commented as is obsolete if no further processing is needed  
        'Selection.Comment.Shape.Select
        'Commented not to fix Comment Aspect Ratio
        'With Selection.ShapeRange       'Fix 2.5 aspect ratio
        '    .LockAspectRatio = msoFalse
        '    MaxSize = .Width / 2.5
        '    If MaxSize > .Height Then
        '        .Height = MaxSize
        '    Else
        '        .Width = .Height * 2.5
        '    End If
        'End With
        'Commented to neglect fonts
        'With Selection.Font
        '    .Bold = False
        '    .Name = "Times New Roman"
        '    .Size = 12
        'End With

        Acl.Comment.Visible = False
    End If
Next
InitRng.Select

End Sub
Run Code Online (Sandbox Code Playgroud)

保留不需要的代码和注释项。我还得弥补还不能处理的合并单元格。

干杯