G
gidmanma
I need to modify the look and feel of tables in a document based on the
styles used within each cell. I have some code doing exactly what I need it
to...until I hit a merged cell. Ughh. Below are two code samples - the
first, FormatTables() works properly but dies a painful death when it
encounters a merged cell. The second, FormatTables2() doesn't die, it just
leaves the background color for the table object and the merged cells set to
black. I have no idea why.
I need something that will work. Any help would be appreciated.
TIA
gidmanma
---------------------------------
Sub FormatTables()
Dim aTable, aRow, aCell, RowCount
For Each aTable In ActiveDocument.Tables
With aTable
.Spacing = InchesToPoints(0.05)
.AllowPageBreaks = True
.AllowAutoFit = True
.Shading.BackgroundPatternColor = wdColorWhite
.Shading.Texture = wdTextureNone
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 96 '98
.Borders.Enable = False
.Rows.Alignment = wdAlignRowLeft
.Rows.LeftIndent = InchesToPoints(0.1) 'InchesToPoints(0) '
.Columns.PreferredWidthType = wdPreferredWidthAuto
End With
RowCount = 1
For Each aRow In aTable.Rows
If RowCount = 1 Then
For Each aCell In aRow.Cells
aCell.Shading.BackgroundPatternColor = RGB(233, 231, 224) '
DCT Tan 70% tint
aCell.Shading.ForegroundPatternColor = wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
'aCell.PreferredWidthType = wdPreferredWidthPercent
'aCell.PreferredWidth = 25
Next aCell
Else
For Each aCell In aRow.Cells
aCell.Shading.BackgroundPatternColor = RGB(248, 247, 245) '
DCT Tan 90% tint
aCell.Shading.ForegroundPatternColor = wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
'aCell.PreferredWidthType = wdPreferredWidthPercent
'aCell.PreferredWidth = 25
Next aCell
End If
RowCount = RowCount + 1
Next aRow
Next aTable
End Sub
---------------------------------
Sub FormatTables2()
If ActiveDocument.Tables.Count = 0 Then
Exit Sub 'Make sure there are tables in the doc...
Else
'Cycle through the tables / cells and check the style of the cell
contents
'Adjust the background colors accordingly:
' Table Heading - RGB(233, 231, 224) DCT Tan 70% tint
' Table Body Text - RGB(248, 247, 245) DCT Tan 90% tint
' Other - No Color (white)
Dim aTable As Table
Dim aRow As Row
Dim aCell As Cell
Dim aStyle As Style
Dim RowCount
For Each aTable In ActiveDocument.Tables
With aTable
.Spacing = InchesToPoints(0.05)
.AllowPageBreaks = True
.AllowAutoFit = True
.Shading.BackgroundPatternColor = wdColorWhite
.Shading.Texture = wdTextureNone
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 96 '98
.Borders.Enable = False
.Rows.Alignment = wdAlignRowLeft
.Rows.LeftIndent = InchesToPoints(0.1) 'InchesToPoints(0) '
.Columns.PreferredWidthType = wdPreferredWidthAuto
End With
'==========================
'Dim temp, msg
'msg = MsgBox("Pause Here", vbOKOnly)
'==========================
Set aCell = aTable.Cell(1, 1)
Do
'==========================
'msg = "Col:" & aCell.ColumnIndex & " Row:" & aCell.RowIndex & " | " &
aCell.Range.Style
'temp = MsgBox(msg, vbOKOnly)
'==========================
' For Each aCell In aTable.Range.Cells
Set aStyle = aCell.Range.Style
'Select Case aCell.Range.Style
Select Case aStyle
Case "Table Heading"
aCell.Shading.BackgroundPatternColor = RGB(233, 231,
224) ' DCT Tan 70% tint
aCell.Shading.ForegroundPatternColor =
wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
Case "Table Body Text"
aCell.Shading.BackgroundPatternColor = RGB(248, 247,
245) ' DCT Tan 90% tint
aCell.Shading.ForegroundPatternColor =
wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
Case Else 'do nothing
aCell.Shading.BackgroundPatternColor = wdColorWhite
aCell.Shading.ForegroundPatternColor =
wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
End Select
'Next aCell
Set aCell = aCell.Next
Loop Until aCell Is Nothing
Next aTable
End If
End Sub
---------------------------------
styles used within each cell. I have some code doing exactly what I need it
to...until I hit a merged cell. Ughh. Below are two code samples - the
first, FormatTables() works properly but dies a painful death when it
encounters a merged cell. The second, FormatTables2() doesn't die, it just
leaves the background color for the table object and the merged cells set to
black. I have no idea why.
I need something that will work. Any help would be appreciated.
TIA
gidmanma
---------------------------------
Sub FormatTables()
Dim aTable, aRow, aCell, RowCount
For Each aTable In ActiveDocument.Tables
With aTable
.Spacing = InchesToPoints(0.05)
.AllowPageBreaks = True
.AllowAutoFit = True
.Shading.BackgroundPatternColor = wdColorWhite
.Shading.Texture = wdTextureNone
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 96 '98
.Borders.Enable = False
.Rows.Alignment = wdAlignRowLeft
.Rows.LeftIndent = InchesToPoints(0.1) 'InchesToPoints(0) '
.Columns.PreferredWidthType = wdPreferredWidthAuto
End With
RowCount = 1
For Each aRow In aTable.Rows
If RowCount = 1 Then
For Each aCell In aRow.Cells
aCell.Shading.BackgroundPatternColor = RGB(233, 231, 224) '
DCT Tan 70% tint
aCell.Shading.ForegroundPatternColor = wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
'aCell.PreferredWidthType = wdPreferredWidthPercent
'aCell.PreferredWidth = 25
Next aCell
Else
For Each aCell In aRow.Cells
aCell.Shading.BackgroundPatternColor = RGB(248, 247, 245) '
DCT Tan 90% tint
aCell.Shading.ForegroundPatternColor = wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
'aCell.PreferredWidthType = wdPreferredWidthPercent
'aCell.PreferredWidth = 25
Next aCell
End If
RowCount = RowCount + 1
Next aRow
Next aTable
End Sub
---------------------------------
Sub FormatTables2()
If ActiveDocument.Tables.Count = 0 Then
Exit Sub 'Make sure there are tables in the doc...
Else
'Cycle through the tables / cells and check the style of the cell
contents
'Adjust the background colors accordingly:
' Table Heading - RGB(233, 231, 224) DCT Tan 70% tint
' Table Body Text - RGB(248, 247, 245) DCT Tan 90% tint
' Other - No Color (white)
Dim aTable As Table
Dim aRow As Row
Dim aCell As Cell
Dim aStyle As Style
Dim RowCount
For Each aTable In ActiveDocument.Tables
With aTable
.Spacing = InchesToPoints(0.05)
.AllowPageBreaks = True
.AllowAutoFit = True
.Shading.BackgroundPatternColor = wdColorWhite
.Shading.Texture = wdTextureNone
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 96 '98
.Borders.Enable = False
.Rows.Alignment = wdAlignRowLeft
.Rows.LeftIndent = InchesToPoints(0.1) 'InchesToPoints(0) '
.Columns.PreferredWidthType = wdPreferredWidthAuto
End With
'==========================
'Dim temp, msg
'msg = MsgBox("Pause Here", vbOKOnly)
'==========================
Set aCell = aTable.Cell(1, 1)
Do
'==========================
'msg = "Col:" & aCell.ColumnIndex & " Row:" & aCell.RowIndex & " | " &
aCell.Range.Style
'temp = MsgBox(msg, vbOKOnly)
'==========================
' For Each aCell In aTable.Range.Cells
Set aStyle = aCell.Range.Style
'Select Case aCell.Range.Style
Select Case aStyle
Case "Table Heading"
aCell.Shading.BackgroundPatternColor = RGB(233, 231,
224) ' DCT Tan 70% tint
aCell.Shading.ForegroundPatternColor =
wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
Case "Table Body Text"
aCell.Shading.BackgroundPatternColor = RGB(248, 247,
245) ' DCT Tan 90% tint
aCell.Shading.ForegroundPatternColor =
wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
Case Else 'do nothing
aCell.Shading.BackgroundPatternColor = wdColorWhite
aCell.Shading.ForegroundPatternColor =
wdColorAutomatic
aCell.Shading.Texture = wdTextureNone
aCell.PreferredWidthType = wdPreferredWidthAuto
End Select
'Next aCell
Set aCell = aCell.Next
Loop Until aCell Is Nothing
Next aTable
End If
End Sub
---------------------------------