Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2007

Tip: Looking for answers? Try searching our database.

Row Formatting Using VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
eded1218@gmail.com - 16 Nov 2007 23:47 GMT
I am a new VBA user.  I would like to format the rows in a Word table
based on the contents of the first cell in the row (column #1), which
will be either "a", "p", or "d" (which represents active, pending or
declined) or the cell could be blank.

So, for example, if the first cell is blank, I don't want to make any
changes to the row's format.  However, if the first cell contains "a",
I would like the font of the entire row to be white, the fill to be
green and the font to be bold.  And so on, with different colors/
formatting for each row that contains a different letter.

This doesn't seem like it should be that difficult, but I haven't
really found that much information on how to work with tables using
VBA.

I would really appreciate any help!

Also, could anyone recommend a good book on using VBA in Word?  Thanks
in advance!
Jay Freedman - 17 Nov 2007 01:20 GMT
This macro could be modified to make it more efficient, but I left
everything spelled out so you can follow it more easily.

Sub demo()
   Dim myTbl As Table
   Dim myRow As Row
   
   ' this assumes there is at least one table
   ' and the first is the one you're formatting
   Set myTbl = ActiveDocument.Tables(1)
   
   ' go through all the rows
   For Each myRow In myTbl.Rows
       If myRow.Cells(1).Range.Characters(1).Text = "a" Then
           myRow.Range.Font.Bold = True
           myRow.Range.Font.Color = wdColorWhite
           myRow.Shading.BackgroundPatternColor = wdColorGreen
       End If
       
       If myRow.Cells(1).Range.Characters(1).Text = "p" Then
           myRow.Range.Font.Bold = False
           myRow.Range.Font.Color = wdColorYellow
           myRow.Shading.BackgroundPatternColor = wdColorRed
       End If
       
       If myRow.Cells(1).Range.Characters(1).Text = "d" Then
           myRow.Range.Font.Bold = True
           myRow.Range.Font.Color = wdColorGray10
           myRow.Shading.BackgroundPatternColor = wdColorBlue
       End If
   Next
End Sub

There are a couple of ways to handle tables in VBA. The first job is
to locate the particular table you want to work on (or else use a For
Each to work on all the tables in the document). That's the effect of
the Set statement.

The table object has several collections that let you work with its
pieces in various ways. This demo shows working with the Rows
collection, by using a For Each statement to cycle through all the
rows. There is also a Columns collection, which would be useful if you
wanted to format all the cells in a column the same way. And there's a
Cells collection, from which you can pick out individual cells by row
and column coordinates.

Within each row, the code checks the text in the first cell in that
row's Cells collection (not to be confused with the table's Cells
collection). Depending on what is there, at most one of the If
statements will have a true condition; if the first cell is blank or
has some other letter in it, then none of the If statements will have
a true condition.

The multiple If statements could be collected into a Select Case
statement for better efficiency.

>I am a new VBA user.  I would like to format the rows in a Word table
>based on the contents of the first cell in the row (column #1), which
[quoted text clipped - 15 lines]
>Also, could anyone recommend a good book on using VBA in Word?  Thanks
>in advance!

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
eded1218@gmail.com - 19 Nov 2007 15:38 GMT
> This macro could be modified to make it more efficient, but I left
> everything spelled out so you can follow it more easily.
[quoted text clipped - 79 lines]
>
> - Show quoted text -

Thank-you!  That's exactly what I needed and it worked perfectly!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.