Word Colors

Posted on 2006-03-23
Last Modified: 2012-06-21
I have a table with color names:


and so on

I open the table and run through a loop.  Based upon criteria, I need to color certain cells of a word table (I am doing this from Access).

if I use this code, specifying the color directly, it works:
                With wdApp.Selection.Cells
                    With .Shading
                        .Texture = wdTextureNone
                        .ForegroundPatternColor = wdColorAutomatic
                        .BackgroundPatternColor = wdColorBrightGreen
                    End With
                End With

But what I want to do is this:

                With wdApp.Selection.Cells
                    With .Shading
                        .Texture = wdTextureNone
                        .ForegroundPatternColor = wdColorAutomatic
                        .BackgroundPatternColor = myTable1!Word_Color_Name
                    End With
                End With

I get a Type Mismatch error.

How can I do this?  Alternatively, where can I find a list of word colors and their associated Color Index numbers so I can use those?

Question by:wsturdev
    LVL 76

    Expert Comment

    You can get a list of Word.Color constants in the VBA editor, by choosing View/Object Browser. Select Word in the top dropdown, and scroll the Classes pane until you get to the wdColor entry. Select it and the list will appear in the right-hand pane.
    LVL 76

    Expert Comment

    The constant must be a Long Integer, as colours always are in Windows.
    VB has a much more limited list, such as vbYellow, which has the same value as wdColorYellow
    LVL 76

    Expert Comment

    Note that there is also a wdColorIndex type of constant which can apply to a ...ColorIndex property. This is a predefined list of 'solid' colours to which text is usually limited.
    LVL 1

    Author Comment

    I did as suggested in your first response, and I see the list of colors.  That is what I have in my table.  Now how do I use the name of the color as listed in the table in a statement?

    if I say:

                           .BackgroundPatternColor = wdColorBrightGreen
     it sets the cell to bright green, but I have to hard code wdColorBrightGreen

    Instead, I want to have a field in Table1 called Word_Color_Name.  Each row of the table will have one entry from the list of color constants.  How do I retrieve the color constant "wdColorBrightGreen" from the field Word_Color_Name and use it dynamically in a statement, as in=:

                            .BackgroundPatternColor = myTable1!Word_Color_Name

    I understand your second post.  Now, is there a place I can look so I can find out what the long integer value is that corresponds to wdColorBrightGreen?

    LVL 76

    Accepted Solution

    You will have to use a funtion with a series of select statements.
    I have just written this by copying the list from the Word VBA Help file and editing it with Word's Find and Replace, using wildcards.
    Function GetColourValue(strName As String) As WdColor
        Dim lValue As Long
        Select Case strName
            Case "wdColorGray625"
                lValue = wdColorGray625
            Case "wdColorGray70"
                lValue = wdColorGray70
            Case "wdColorGray80"
                lValue = wdColorGray80
            Case "wdColorGray875"
                lValue = wdColorGray875
            Case "wdColorGray95"
                lValue = wdColorGray95
            Case "wdColorIndigo"
                lValue = wdColorIndigo
            Case "wdColorLightBlue"
                lValue = wdColorLightBlue
            Case "wdColorLightOrange"
                lValue = wdColorLightOrange
            Case "wdColorLightYellow"
                lValue = wdColorLightYellow
            Case "wdColorOliveGreen"
                lValue = wdColorOliveGreen
            Case "wdColorPaleBlue"
                lValue = wdColorPaleBlue
            Case "wdColorPlum"
                lValue = wdColorPlum
            Case "wdColorRed"
                lValue = wdColorRed
            Case "wdColorRose"
                lValue = wdColorRose
            Case "wdColorSeaGreen"
                lValue = wdColorSeaGreen
            Case "wdColorSkyBlue"
                lValue = wdColorSkyBlue
            Case "wdColorTan"
                lValue = wdColorTan
            Case "wdColorTeal"
                lValue = wdColorTeal
            Case "wdColorTurquoise"
                lValue = wdColorTurquoise
            Case "wdColorViolet"
                lValue = wdColorViolet
            Case "wdColorWhite"
                lValue = wdColorWhite
            Case "wdColorYellow"
                lValue = wdColorYellow
            Case "wdColorAqua"
                lValue = wdColorAqua
            Case "wdColorAutomatic"
                lValue = wdColorAutomatic
            Case "wdColorBlack"
                lValue = wdColorBlack
            Case "wdColorBlue"
                lValue = wdColorBlue
            Case "wdColorBlueGray"
                lValue = wdColorBlueGray
            Case "wdColorBrightGreen"
                lValue = wdColorBrightGreen
            Case "wdColorBrown"
                lValue = wdColorBrown
            Case "wdColorDarkBlue"
                lValue = wdColorDarkBlue
            Case "wdColorDarkGreen"
                lValue = wdColorDarkGreen
            Case "wdColorDarkRed"
                lValue = wdColorDarkRed
            Case "wdColorDarkTeal"
                lValue = wdColorDarkTeal
            Case "wdColorDarkYellow"
                lValue = wdColorDarkYellow
            Case "wdColorGold"
                lValue = wdColorGold
            Case "wdColorGray05"
                lValue = wdColorGray05
            Case "wdColorGray10"
                lValue = wdColorGray10
            Case "wdColorGray125"
                lValue = wdColorGray125
            Case "wdColorGray15"
                lValue = wdColorGray15
            Case "wdColorGray20"
                lValue = wdColorGray20
            Case "wdColorGray25"
                lValue = wdColorGray25
            Case "wdColorGray30"
                lValue = wdColorGray30
            Case "wdColorGray35"
                lValue = wdColorGray35
            Case "wdColorGray375"
                lValue = wdColorGray375
            Case "wdColorGray40"
                lValue = wdColorGray40
            Case "wdColorGray45"
                lValue = wdColorGray45
            Case "wdColorGray50"
                lValue = wdColorGray50
            Case "wdColorGray55"
                lValue = wdColorGray55
            Case "wdColorGray60"
                lValue = wdColorGray60
            Case "wdColorGray65"
                lValue = wdColorGray65
            Case "wdColorGray75"
                lValue = wdColorGray75
            Case "wdColorGray85"
                lValue = wdColorGray85
            Case "wdColorGray90"
                lValue = wdColorGray90
            Case "wdColorGreen"
                lValue = wdColorGreen
            Case "wdColorLavender"
                lValue = wdColorLavender
            Case "wdColorLightGreen"
                lValue = wdColorLightGreen
            Case "wdColorLightTurquoise"
                lValue = wdColorLightTurquoise
            Case "wdColorLime"
                lValue = wdColorLime
            Case "wdColorOrange"
                lValue = wdColorOrange
        End Select
        GetColourValue = lValue
    End Function


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
    Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
    This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
    This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now