Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-14-2017, 05:14 PM
PMC11 PMC11 is offline Replacing wdColors with RGB colors in my Macro Windows 7 32bit Replacing wdColors with RGB colors in my Macro Office 2010 32bit
Novice
Replacing wdColors with RGB colors in my Macro
 
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
  #2  
Old 11-14-2017, 06:32 PM
macropod's Avatar
macropod macropod is offline Replacing wdColors with RGB colors in my Macro Windows 7 64bit Replacing wdColors with RGB colors in my Macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can't use ColorIndex or wdColor for colours other than those specified by ColorIndex & wdColor constants, respectively. hence, you'll need to replace:
Code:
cll.Range.Font.ColorIndex
with:
Code:
cll.Range.Font.Color
but then you'll still only be able to use the wdColor for colours included in the wdColor constants - for anything else you'll need to get their RGB values and use those instead.

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-16-2017, 03:42 AM
slaycock slaycock is offline Replacing wdColors with RGB colors in my Macro Windows 7 64bit Replacing wdColors with RGB colors in my Macro Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

A note about an intellisense annoyance. The property 'color' no longer appears in the list of properties but is valid if you actually type it in.

You can get to the hex definition of a color by looking at the values of the various items in the wdColor enumeration (View.Object Browser then type wdColor in the search box)

e.g. wdColorBlue has the hex value &HFF0000.

Note wdColorBlue is not the same as wdBlue as wdBlue is defined in the wdColorIndex enumeration and has the value 2.

Also note that the hex value &HFF0000 is in the format &HBBGGRR

So the RGB for wdColorBlue is RGB(&H00, &H00, &HFF)
Reply With Quote
Reply

Tags
macro, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing wdColors with RGB colors in my Macro VBA combination drop down macro - changing colors runke1tj Word VBA 2 04-27-2017 07:55 AM
Replacing wdColors with RGB colors in my Macro Help with macro to set font colors Jennifer Murphy Word VBA 1 09-29-2013 09:57 PM
Replacing texts aslamyahya Excel Programming 1 02-27-2013 10:38 PM
Macro for replacing, copying and undoing. vthomeschoolmom Word VBA 1 12-05-2012 07:41 AM
Replacing wdColors with RGB colors in my Macro Unable to change font colors from theme colors choy Word 3 08-01-2012 09:12 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:54 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft