Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shading Select Table with VBA

Posted on 2011-09-17
2
Medium Priority
?
408 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
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…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

610 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