在 Word 2016 VBA 中,我想使用循环设置表格的每个单元格的阴影。这似乎适用于大小约为 15*15 的表格。对于 20*20 或更大的表格,Word 不再响应。尽管在使用单步时,程序执行似乎是正确的。我试过这个 ca 表。50*50。ScreenRefresh 和 ScreenUpdating 似乎没有影响。在代码示例中,将每个单元格的底纹设置为相同的背景色只是为了演示,最后我想应用更复杂的设置。
Sub TableCells_SetBackgroundColors()
' Set background color for each cell in Word table
' Application does not respond if table is larger than about 20*20
' debug single step works in any case
'Application.ScreenUpdating = False
Dim i, k, cntCol, cntRow As Integer
cntCol = 15 ' 20 is not ok
cntRow = 15 ' 20 is not ok
If ActiveDocument.Tables.Count <> 0 Then
ActiveDocument.Tables(1).Delete
End If
ActiveDocument.Tables.Add Range:=Selection.Range, _
numRows:=cntRow, _
NumColumns:=cntCol
Dim myTable As Word.Table
Set myTable = Selection.Tables(1)
With myTable.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleSingle
End With
For i = 1 To cntRow Step 1
For k = 1 To cntCol Step 1
myTable.Cell(i, k).Shading.BackgroundPatternColor = wdColorRed
'Application.ScreenRefresh
Next k
Next i
'Application.ScreenUpdating = True
End Sub