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