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

我有一个电子表格,它是以编程方式创build的,其中有很多注释(高达40,000)。 从工作表中删除多个列之后,这些注释会被resize。 这显然是一个Excel中的错误。 ( http://answers.microsoft.com/zh-cn/office/forum/office_2007-excel/excel-comment-boxes-resizing-themselves-andor/3fdf3e72-6ca5-4186-a656-b7b6fd8db781?msgId=d55534a5-4603 -482e-ac97-9ec260124f78 )

理想情况下,我想在删除列后立即AutoSize全部注释。

试图避免循环每个单独的评论,这是我迄今为止尝试过的。

  • 设置AutoShapeDefaults不起作用 – 删除列后,评论仍将resize。
  • XlPlacement属性。 XlMove和XLMoveAndSize不起作用。
  • 无论注释量如何,Worksheet.Shapes.SelectAll都会抛出OutOfMemoryexception

我的想法是获取电子表格中所有注释的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; } 

更改为在AutoSize行引发exception“来自HRESULT:0x800A03EC的exception”。

  ShapeRange sRange = objSheet.Shapes.Range[test]; sRange.TextFrame.AutoSize = true; 

使用我的实际数组形状指数抛出相同的exception,但在Shapes.Range [] 。 我在debugging时看过形状variables,除了int [249]而不是int [5] ;

  int[] shapes = (int[])shapes.ToArray(typeof(int)); ShapeRange sRange = objSheet.Shapes.Range[shapes]; 

那么我会回答与必须在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 

保持代码和评论项目不需要。 我仍然必须化妆合并单元格,目前还无法处理。

干杯