Solved

vba for coloring Word cells in a table

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
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 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…
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.

708 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

13 Experts available now in Live!

Get 1:1 Help Now