loop through tables in word and change color of cells
Hello forum,
I would like to have suggestions on how to change color in tables using VBA. The word file has many tables but all have the same structure, only the contents change. I need a macro which changes the background color of the cells according to the contents. So for example, if the cell contains the text "I" then I want that cell colored in blue, if insted it contains "V" then it should be red and so on for 8 different classes. The following is the macro I modified (the original one was developed by someone else) but when I run it nothing happen, and I don't understand where I'm wrong.
Thank you for any help!
Daniel
Code:
Sub colora()
Dim MyTable As Table
Dim MyRow As Row
Dim MyCell As Cell
For Each MyTable In ActiveDocument.Tables
For Each MyRow In MyTable.Rows
For Each MyCell In MyRow.Cells
If MyCell.Range.Text = "I" Then
MyCell.Shading.BackgroundPatternColor = wdColorBlue
ElseIf MyCell.Range.Text = "V" Then
MyCell.Shading.BackgroundPatternColor = wdColorRed
End If
Next MyCell
Next MyRow
Next MyTable
Set MyCell = Nothing
Set MyRow = Nothing
Set MyTable = Nothing
End Sub
Please note the crossposting below with the same question but differently formulated
https://groups.google.com/g/microsof.../c/YLWdSId_14k