Solved

Shading Select Table with VBA

Posted on 2011-09-17
2
394 Views
Last Modified: 2012-05-12
I have a document previously generated by a mail merge.  Each page is identical other than the data filled in by the mail merge.  

I would like a VBA routine that reads the table on the top right of each page to determine the color based on the text in that table.   Either Green, Blue, Yellow or White.  Once the color is determined, fill in that table with the color plus the three tables on that page that describe the student's name and their car.   If the color is White, then do nothing on that page.   Pages 1 and 3 have the results I am trying to accomplish.

I have some VBA experience with Excel but I have never written any VBA for WORD.  Any help would be appreciated.

Word 2007 or Word 2010

Thanks,
ProdOps
 EE-Table-Shading.docx
0
Comment
Question by:Jerry Paladino
2 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 36555938
Assuming the structure is consistent then the following should do it.  You will need to remove the existing highlight color if you change the color constants ... i've deliberately used the  same ones for blue and green.  But included a comment in each case for a potential word constant to make re-use easier ... simply delete the cb constant and the tick to leave the word color constant.

Chris
Sub colorme()
Dim tbl As Table
Dim lngColorBy As Long
Const cbGreen As Long = 5296274
Const cbBlue As Long = 15773696
Const cbYellow As Long = 10092543
    
    For Each tbl In ActiveDocument.Tables
        If InStr(1, tbl.Cell(1, 2).Range.Text, "run group", vbTextCompare) > 0 Then
            If InStr(1, tbl.Cell(1, 2).Range.Text, "green", vbTextCompare) > 0 Then
                lngColorBy = cbGreen 'wdColorLightGreen
            ElseIf InStr(1, tbl.Cell(1, 2).Range.Text, "blue", vbTextCompare) > 0 Then
                lngColorBy = cbBlue ' wdColorPaleBlue
            ElseIf InStr(1, tbl.Cell(1, 2).Range.Text, "yellow", vbTextCompare) > 0 Then
                lngColorBy = cbYellow 'wdColorLightYellow
            Else
                lngColorBy = wdColorAutomatic
            End If
            tbl.Cell(1, 2).Shading.BackgroundPatternColor = lngColorBy
            tbl.Cell(6, 1).Shading.BackgroundPatternColor = lngColorBy
        Else
            tbl.Cell(1, 1).Shading.BackgroundPatternColor = lngColorBy
        End If
    Next

End Sub

Open in new window

0
 
LVL 16

Author Closing Comment

by:Jerry Paladino
ID: 36556340
Chris,

Thank you!  This works great.  It took me a bit to understand how the table cell references were addressed in Word but once I got that it makes perfect sense.

Thank you again!
Jerry
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

746 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

14 Experts available now in Live!

Get 1:1 Help Now