Solved

vba for coloring Word cells in a table

Posted on 2010-09-03
3
3,231 Views
Last Modified: 2012-05-10
Looking for help with a VBA script that will set the background color of a table to a keyword in each cell.

The procedure is as follows:
1. step through each cell of a Word table
2. locate the color keyword (green, red, blue, etc) in the cell
3. set the cell background color to the color keyword found in step 3
4. delete the color keyword from the cell, but leave any other tex

Thanks in advance for the help
0
Comment
Question by:johnfaig
  • 2
3 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33599874
Let me get this straight. You have a word document which contains a table
The table has multiple columns and rows and each cell has a word in it
If this word is the name of a color you want the background color of that cell to change to that text?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33600172
johnfaig,Not to be a pain or anything, but what if the other text in that cell has text that looks like a color, but is not?Otis ReddingBlue Sky SoftwareI played Greensleeves on the piano, but she wasn't all that impressedAlso, what version of Word is this for?  Office 2007 made radical changes to how coloring is done...Patrick
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 33600373
Good question Patrick (by the way long time no e-see, how you keeping?)

I am assuming it is just the one word. I tried some code and it seemed the word ends with a carriage return
Not so elegant in that you have to map text to a enum value, there are probably better ways but for now just give this a go

You will need to expand on the case statement
this vba would be in your word doc



Public Sub UpdateTable()

    Dim i As Integer
    Dim j As Integer
    Dim c As String
   
   
    ActiveDocument.Tables(1).Select
   
    For i = 1 To ActiveDocument.Tables(1).Rows.Count
        For j = 1 To ActiveDocument.Tables(1).Columns.Count
            ActiveDocument.Tables(1).Cell(i, j).Select
            c = Trim$(LCase$(ActiveDocument.Tables(1).Cell(i, j).Range.Text))
            c = Left$(c, InStr(1, c, Chr$(13)) - 1)
            Select Case (c)
                Case "black"
                    Selection.Cells.Shading.BackgroundPatternColor = wdColorBlack
                Case "blue"
                    Selection.Cells.Shading.BackgroundPatternColor = wdColorBlue
                Case "green"
                    Selection.Cells.Shading.BackgroundPatternColor = wdColorGreen
                Case "yellow"
                    Selection.Cells.Shading.BackgroundPatternColor = wdColorYellow
                Case "red"
                    Selection.Cells.Shading.BackgroundPatternColor = wdColorRed
            End Select
        Next j
    Next i
End Sub
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

749 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