?
Solved

Shading Select Table with VBA

Posted on 2011-09-17
2
Medium Priority
?
406 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
[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 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 23 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