Solved

vba for coloring Word cells in a table

Posted on 2010-09-03
3
3,154 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
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.

776 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