Re-addressing Custom Table Styles

R

RFox

I started a thread a couple weeks ago asking about how
to automatically alternate row shading.
Been spending some time in this newsgroup since then and
have found that Word XP would allow me to do this as well
as customize my own Styles.

Alas, I have Word 2000, which means that I either have
to stick with the Table Style combinations pre-defined
by the program, or to manually format each and every
one of my numerous tables.

I've been thinking, how about setting up a macro to
do the table format?! Unfortunately, I don't have any
experience with visual basic.

Best I could come up with by recording my actions into
a macro are thibgs that apply to the entire selection
of the table, such as cell margins, borders, global
text styles.

I'm thinking there's got to be a way to specify
"Selection.Tables" with ".Rows.First" or ".Rows(1)"
to stylize the header text and colorized the cells.

And I'm thinking using a For/Next loop to count
off the even numbered rows to give the row a different
shading color.

But I can't get it to work because I can't seem to
use "Syles" with "Selection.Table"

Can anyone with more experience help?
 
S

Shauna Kelly

Hi R

This should get you going. In the Visual Basic Editor, at Tools > Options, make sure you have ticked Auto List Members and Auto
Quick Info and Auto Data Tips. If those are ticked, Word will (usually) help you by letting you know what options are available. For
example, to get a different colour of shading, delete = wdColorGray125, then go back and type = and it will pop up a list of valid
choices.


Sub ShadeRowsInTables()
'Shauna Kelly, 12 September 2003
'For microsoft.public.word.tables

Dim oTable As Table
Dim nCounter As Long

If Not Selection.Information(wdWithInTable) Then
MsgBox "This macro only works if your cursor is in a table."
Else
If MsgBox("This macro will shade every second row of your table." _
& vbCrLf & vbCrLf & "Proceed?", vbYesNo) = vbYes Then

Set oTable = Selection.Tables(1)
For nCounter = 2 To oTable.Rows.Count Step 2
oTable.Rows(nCounter).Range.Shading.BackgroundPatternColor = wdColorGray125
Next nCounter
End If
End If
End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
Melbourne, Australia
 
R

RFox

Wow! You can't imagine how much time and effort you've
saved me!

I just stuck in your subroutine in with the rest of my
macro, and placed the macro as a button on the toolbar.
Now it takes me 1 button click to format my tables instead
of multiple clicks and properties boxes, etc.

You are totally aweseome! Thank you.

There's just one more thing missing in my macro formatting,
and that's setting the table header with a different
Style, which is basically the same as the table body
Style but bolded.

Is there an object property to specify setting a style to
only the first row when you've selected the entire table?

I figure it has to be something like:

Selection.Tables(1).Rows(1). [...?...] =
ActiveDocument.Styles("TableHeader")

The [?] area is where I'm stumped.
I do have the Auto List function enabled. That's a great
feature. But it doesn't list any kind of Style property
available for Tables or Rows. When I look at the Help,
it says that these objects are read-only?
I've tried using "Style" anyway, and not surprisingly it
gives a compile error.

Can you help?

Mucho thanks to you.
 
R

RFox

Hi Shauna,

I used your code as an example and built my statement as follows:

Selection.Tables(1).Rows(1).Range.Style =
ActiveDocument.Styles("TableHeader")

I don't know why the Range property is needed when I've
already specified the first row in Rows(1).
But it works!

Now I've got my own defined custom Table style!

I am so stoked! And to think I've been formatting my tables
by hand all this time!

Hey, everyone, if you are not using Word 2002 and need to
create your own table style for easy autoformatting, create
a macro to do it for you!
 
S

Shauna Kelly

Hi R

I'm glad it worked. And I see you've already found the solution to applying a style to the first row.

Shauna


Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
Melbourne, Australia


RFox said:
Wow! You can't imagine how much time and effort you've
saved me!

I just stuck in your subroutine in with the rest of my
macro, and placed the macro as a button on the toolbar.
Now it takes me 1 button click to format my tables instead
of multiple clicks and properties boxes, etc.

You are totally aweseome! Thank you.

There's just one more thing missing in my macro formatting,
and that's setting the table header with a different
Style, which is basically the same as the table body
Style but bolded.

Is there an object property to specify setting a style to
only the first row when you've selected the entire table?

I figure it has to be something like:

Selection.Tables(1).Rows(1). [...?...] =
ActiveDocument.Styles("TableHeader")

The [?] area is where I'm stumped.
I do have the Auto List function enabled. That's a great
feature. But it doesn't list any kind of Style property
available for Tables or Rows. When I look at the Help,
it says that these objects are read-only?
I've tried using "Style" anyway, and not surprisingly it
gives a compile error.

Can you help?

Mucho thanks to you.


Shauna Kelly said:
Hi R

This should get you going. In the Visual Basic Editor, at Tools > Options, make sure you have ticked Auto List Members and Auto
Quick Info and Auto Data Tips. If those are ticked, Word will (usually) help you by letting you know what options are available. For
example, to get a different colour of shading, delete = wdColorGray125, then go back and type = and it will pop up a list of valid
choices.


Sub ShadeRowsInTables()
'Shauna Kelly, 12 September 2003
'For microsoft.public.word.tables

Dim oTable As Table
Dim nCounter As Long

If Not Selection.Information(wdWithInTable) Then
MsgBox "This macro only works if your cursor is in a table."
Else
If MsgBox("This macro will shade every second row of your table." _
& vbCrLf & vbCrLf & "Proceed?", vbYesNo) = vbYes Then

Set oTable = Selection.Tables(1)
For nCounter = 2 To oTable.Rows.Count Step 2
oTable.Rows(nCounter).Range.Shading.BackgroundPatternColor = wdColorGray125
Next nCounter
End If
End If
End Sub


Hope this helps.
 
J

JGM

Hi Fox
I don't know why the Range property is needed when I've
already specified the first row in Rows(1).

Because the statement
Selection.Tables(1).Rows(1)
Just means that in the current selection, take the first row of the first
table. You still have to tell the compiler what you are going to do with
that row. You know that you want to shade every other row, but the computer
is dumb, it cannot read your mind, so you have to be as specific as
possible.

For example, you could delete that row:
Selection.Tables(1).Rows(1).Delete

or you could select some cells in that row:
Selection.Tables(1).Rows(1).Cells(1)

or you could do something to its borders:
Selection.Tables(1).Rows(1).Borders
and so on.

Selection.Tables(1).Rows(1).Range
means that you want to do something to the whole row.

HTH
Cheers!
 
R

RFox

JGM said:
Hi Fox


Because the statement
Selection.Tables(1).Rows(1)
Just means that in the current selection, take the first row of the first
table. You still have to tell the compiler what you are going to do with
that row. You know that you want to shade every other row, but the computer
is dumb, it cannot read your mind, so you have to be as specific as
possible.

For example, you could delete that row:
Selection.Tables(1).Rows(1).Delete

or you could select some cells in that row:
Selection.Tables(1).Rows(1).Cells(1)

or you could do something to its borders:
Selection.Tables(1).Rows(1).Borders
and so on.

Selection.Tables(1).Rows(1).Range
means that you want to do something to the whole row.

I understand that you need to be as specific as possible.
I keep trying things like:
Selection.Tables(1).Rows(1).Style

to apply a style to the row, which of course, did not
work. I did not know that the Range property specified
the entire row selection.

After all, the application help defined the Ranger Property:
"Returns a Range object that represents the portion of a
document that's contained in the specified object. Read-only."

Which really doesn't tell me anything.
And I thought that to define a Range object, you have to, well,
specify a start and end point to the range.
And When I specified "Selection.Tables(1).Rows(1).....
I thought it meant I wanted the entire row already.

Anyway, I sure there are many remarkable things you can
do with Word by creating macros like this.
Unfortunately, I don't have the time or much inclination to
learn Visual Basic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top