PDA

View Full Version : Formating tables



pb001
07-06-2011, 02:57 AM
Hi

I am creating a document template that has a number of tables within it. I can format all tables with the same heading row (HeadingFormat), set the Background color etc... My problem is I don't want to change the format of the first 3 tables in the document.

This is the code I am using:
If ActiveDocument.Tables.Count >= 1 Then
Dim objTable As Word.Table
For Each objTable In ActiveDocument.Tables
objTable.Rows(1).HeadingFormat = True
objTable.Rows(1).Shading.BackgroundPatternColor = RGB(149, 179, 215)
Next objTable
Set objTable = Nothing
End If

Is there an easy way to do this?
Thanks

Frosty
07-06-2011, 07:33 AM
Instead of using a For...Each loop, use a For... loop to iterate through your tables. Something along the lines of...

Dim i as integer
For i = 4 to ActiveDocument.Tables.Count
Set objTable = ActiveDocument.Tables(i)
'rest of your code
Next


Incidentally, you don't need to test for a tables.count above 0 when you're using either of these loops. It will automatically skip the entire structure if there are no tables (in the for each loop) or if i is greater than the activedocument.tables.count.

And you also don't need to set your objTable to nothing in the For Each...loop (although it's a good habit) as once you're out of that loop, your object becomes nothing.

You do need to set it to nothing in the For... Loop because it will be left set to the last table in the document. Although you could use
With ActiveDocument.Tables(i)
End With

Within the For...Loop and skip dimensioning/setting the objTable as well.

Hope this helps.

pb001
07-06-2011, 07:42 AM
Hi
That worked perfectly - many thanks.