Reputation: 1
I have been trying to find a code to format the first table of a word document a specific way and then loop through the rest of the tables formatting them another way. I found a code that loops through all of the tables correctly, but haven't found a way to exclude the first table in order to format it differently. This is the code that works for all of the tables
Sub Change_All_Tables_Formating()
Dim oTbl As Word.Table
Dim oCell As Word.Cell
For Each oTbl In ActiveDocument.Tables
For Each oCell In oTbl.Range.Cells
oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next oCell
oTbl.Rows(1).Shading.BackgroundPatternColorIndex = wdGray25
oTbl.Rows(1).Range.Bold = True
Next oTbl
End Sub
This is the code that I am having trouble adapting to format the first table correctly. It needs to remove the shading from the first table completely and shade the headings of the rest of the tables in the word document. I feel that if I can learn how to do this, I will be able to adjust any of the tables that I would like and format them individually. The number of tables vary with each document, but the first table is always the same. I will appreciate any help that anyone can provide.
Sub Change_Tables_Formating()
Dim oTbl As Word.Table
Dim oCell As Word.Cell
Set oTbl = ActiveDocument.Tables
Set oTblcnt = wdDoc.Tables.Count
For oTbl = 0 To 1
For Each oCell In oTbl.Range.Cells
oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
Next oCell
oTbl.Range.Shading.BackgroundPatternColorIndex = wdColorWhite
Next oTbl
For oTbl = 2 To oTblct
For Each oCell In oTbl.Range.Cells
oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next oCell
oTbl.Rows(1).Shading.BackgroundPatternColorIndex = wdGray25
oTbl.Rows(1).Range.Bold = True
Next oTbl
End Sub
Upvotes: 0
Views: 1278
Reputation: 166146
Here's how I'd do it:
Sub Change_Tables_Formating()
Dim oTbl As Word.Table, i As Long
For Each oTbl In ActiveDocument.Tables
i = i + 1
If i = 1 Then
FormatFirst oTbl
Else
FormatRest oTbl
End If
Next oTbl
End Sub
Sub FormatFirst(tbl As Table)
Dim oCell
For Each oCell In tbl.Range.Cells
oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
Next oCell
tbl.Range.Shading.BackgroundPatternColorIndex = wdColorWhite
End Sub
Sub FormatRest(tbl As Table)
Dim oCell
For Each oCell In tbl.Range.Cells
oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next oCell
tbl.Rows(1).Shading.BackgroundPatternColorIndex = wdGray25
tbl.Rows(1).Range.Bold = True
End Sub
Upvotes: 1