ใน Word 2016 VBA ฉันต้องการตั้งค่าการแรเงาของแต่ละเซลล์ของตารางด้วยการวนซ้ำ ดูเหมือนว่าจะใช้ได้กับตารางที่มีขนาดสูงสุดประมาณ 15*15 ด้วยตารางเช่น 20*20 หรือใหญ่กว่า Word จะไม่ตอบสนองอีกต่อไป ดูเหมือนว่าการทำงานของโปรแกรมจะถูกต้องแม้ว่าจะใช้ขั้นตอนเดียวก็ตาม ฉันลองสิ่งนี้กับตารางประมาณ 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