?
Solved

vba for coloring Word cells in a table

Posted on 2010-09-03
3
Medium Priority
?
3,379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 93

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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
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…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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