0

I have a table in Word built from repeating section content control. There are text content controls in cells of repeating section of CC.

I am able to give font color based on the text; but I couldn't change the shading of the cell except the last row.

In debug session, whether I see correct row and column number, Shading.BackgroundPatternColor does not change the color. Surprisingly, it works on the last row of the table.

Dim CC As ContentControl
Dim TableNum As Long, RowNum As Long, ColNum As Long

For Each CC In ActiveDocument.ContentControls
    If CC.Tag = "tagPriority" Then
        If CC.Range.Text = "Critical" Then
            CC.Range.Font.TextColor = wdColorAutomatic
            If CC.Range.Information(wdWithInTable) Then
                  TableNum = Me.Range(0, CC.Range.End).Tables.Count
                  RowNum = CC.Range.Information(wdStartOfRangeRowNumber)
                  ColNum = CC.Range.Information(wdStartOfRangeColumnNumber)
                  ActiveDocument.Tables(TableNum).Cell(RowNum, ColNum).Shading.BackgroundPatternColor = wdColorDarkRed
             End If
             ...

Also, I got help from the code in stackoverflow

1 Answers1

0

After changing

TableNum = Me.Range(0, CC.Range.End).Tables.Count

to

TableNum = ActiveDocument.Range(0, CC.Range.End).Tables.Count

it worked for me. I put your code in a standard module so YMMV if you have your code in a document event handler in the ThisDocument module.

Of course there is another method to get the table which also works

Dim tbl As Table
Set tbl = CC.Range.Tables(1)
tbl.Cell(RowNum, ColNum).Shading.BackgroundPatternColor = wdColorDarkRed

EDIT: The code I used:

Sub AddCellShading()
   Dim CC As ContentControl
   Dim tbl As Table, RowNum As Long, ColNum As Long

   For Each CC In ActiveDocument.ContentControls
      If CC.Tag = "tagPriority" Then
         If CC.Range.Text = "Critical" Then
            CC.Range.Font.TextColor = wdColorAutomatic
            If CC.Range.Information(wdWithInTable) Then
               Set tbl = CC.Range.Tables(1)
               RowNum = CC.Range.Information(wdStartOfRangeRowNumber)
               ColNum = CC.Range.Information(wdStartOfRangeColumnNumber)
               tbl.Cell(RowNum, ColNum).Shading.BackgroundPatternColor = wdColorDarkRed
            End If
         End If
      End If
   Next CC
End Sub

The result: enter image description here

Timothy Rylatt
  • 7,221
  • 2
  • 10
  • 14
  • Thanks but it didn't work completely, it does work if you have one row, but if there is more than one row, it only changes the last row. – Barış Aybar Oct 21 '20 at 15:27
  • @BarışAybar - Your code will only match content controls where "Critical" is the full text. If there are any extra characters, e.g. a space or a period, your code will fail. – Timothy Rylatt Oct 21 '20 at 20:42
  • Thanks @timothy , your code works if you place Text CC's in standart table cells. However, for my case, table has repeating-section content control on rows, and the Text CCs are placed in those repeating CC cells. I cannot attach links in the document but I guess I can share the link this -> but it https://ibb.co/HTmTw4b You can add repeating sections by selecting a row of table and click on bottom-left of Controls section in the ribbon, then you can add in the cells of it. – Barış Aybar Oct 22 '20 at 11:24
  • @BarışAybar - the table in my test document was set up exactly the same. The code works but will only match content controls where "Critical" is the full text. If there are any extra characters, e.g. a space or a period, your code will fail as evidenced by the screenshot above. – Timothy Rylatt Oct 22 '20 at 13:41
  • I resolved my case, thanks for the support. My table has a normal header row, and repeating CC for the data; and your code was now working. But when I change it to two tables; one for header row, and one for repeating CC, code works. Many thanks. – Barış Aybar Oct 23 '20 at 06:57