View Single Post
 
Old 04-15-2024, 03:40 AM
Italophile Italophile is offline Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 538
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

If the code is in the Normal template then it will already be trusted.

What does "when I open a document from the template it doesn't work" mean exactly?

When I run your code:
Code:
Sub ColourTable()


    Dim objTable As Table
    Dim iRowCounter As Integer
    Dim iColumnCounter As Integer

    Dim lngBadColor As Long
    Dim lngGoodColor As Long
    ' Predefine colors
    lngBadColor = RGB(255, 0, 0)
    lngGoodColor = RGB(169, 208, 142)

    ' Loop through all tables in document
    For Each objTable In ActiveDocument.Tables

        For iRowCounter = 1 To objTable.Rows.count

            For iColumnCounter = 1 To objTable.Columns.count

                ' Get Table cell
                With objTable.Cell(iRowCounter, iColumnCounter)

                    If InStr(.Range.Text, "TO BE DONE") > 0 Then .Shading.BackgroundPatternColor = lngBadColor
                    If InStr(.Range.Text, "Yes") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor
                    If InStr(.Range.Text, "N/A") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor
                    If InStr(.Range.Text, "None") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor

                    ' If InStr(.Range.Text, "Very good") > 0 Then .Shading.BackgroundPatternColor = lngVeryGoodColor
                    ' If InStr(.Range.Text, "Minor") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor

                End With

            Next

        Next

    Next

End Sub
I get
Attached Images
File Type: png Screenshot 2024-04-15 113945.png (9.5 KB, 17 views)
Reply With Quote