View Single Post
 
Old 11-14-2017, 05:14 PM
PMC11 PMC11 is offline Windows 7 32bit Office 2010 32bit
Novice
 
Join Date: Nov 2017
Posts: 2
PMC11 is on a distinguished road
Default Replacing wdColors with RGB colors in my Macro

Hi,

I have the following code that works perfectly. But I need to use custom colours for my eventual goal, so I can't be limited to the standard wdColors like wdRed or wdViolet. When I replace the wdColors with RGB values I get a runtime error 5483, stating that one the values is out of range.

Could any please suggest a way to correct this code so I can replace the colours with RGB values?

Code:
Sub ColourCell()
'
' ColourCell Macro
'
'
    Dim tbl As Table
    Dim rw As Row
    Dim cll As Cell
    Dim i As Long
    Dim Keywords As Variant, Colors As Variant

    'if you have more than one table, you have to look through them
    For Each tbl In ActiveDocument.Tables

    'Make two arrays - one with keywords and the second with colors
    'where the colors are in the same position in their array as the
    'keywords are in theirs
    Keywords = Array("$", "%", "!", "^", "@", "#", "&", "+")
    Colors = Array(RGB(255, 0, 0), wdDarkBlue, wdBlue, wdViolet, wdBrightGreen, wdGreen, wdDarkYellow, wdDarkYellow)

    'Loop through every row in the table
    For Each rw In tbl.Rows
        'Loop through every cell in the row
        For Each cll In rw.Cells
            'Loop through every keyword in your array
            For i = LBound(Keywords) To UBound(Keywords)
                'if the keyword exist, change the color and stop checking
                'further keywords
                If InStr(1, cll.Range.Text, Keywords(i)) > 0 Then
                    cll.Range.Font.ColorIndex = Colors(i)
                    Exit For
                End If
            Next i
        Next cll
    Next rw
Next tbl
End Sub
Thanks very much in advance.

Last edited by macropod; 11-14-2017 at 06:23 PM. Reason: Added code tags
Reply With Quote