Solved

vba for coloring Word cells in a table

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

Technology Partners: 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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

696 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