Forum Discussion
Table Styles and the Ribbon Table Design>Table Option Checkboxes
I have been dabbling in Word VBA for almost three decades but have a fairly limited practical use for Word so I rarely find myself dealing with Tables.
However, the other day I got interested in Charles Kenyon's post on creating a table style with VBA and that led to my discovery of some very odd behavior with both the Table Option Checkboxes on the Ribbon and table style visibility. Specifically a) What triggers which of those six buttons is checked when a table style is applied, b) Why can't one or more of the plethora of "Table Styles" be removed from the Table Style Gallery on the Table Design Ribbon and c) Why can't the table style visibility be modified in the Style Manager "Recommended Dialong ("Show, Hide, Hide Until Used" is dimmed).
First, I took Charles' recorded macro and modified it to focused on only two conditional formatting object “Header Row” and “Total Row”
Option Explicit
Sub TableStyleCreate()
Dim oStyle As Style
Dim oTS As TableStyle
On Error Resume Next
If ActiveDocument.Tables.Count = 1 Then ActiveDocument.Tables(1).Delete 'For testing.
DoEvents
Set oStyle = ActiveDocument.Styles.Add("Table with Just One Condtional Style Element", wdStyleTypeTable)
If Err.Number <> 0 Then
'The named style already exists. Delete it.
ActiveDocument.Styles("Table with Just One Condtional Style Element").Delete
DoEvents
Set oStyle = ActiveDocument.Styles.Add("Table with Just One Condtional Style Element", wdStyleTypeTable)
End If
oStyle.BaseStyle = "Table Grid"
On Error GoTo 0
Set oTS = oStyle.Table
With oTS.Condition(wdFirstRow) 'Associated with Header Row Checkbox
.Shading.BackgroundPatternColor = wdColorLightTurquoise
DefineBorder .Borders(wdBorderLeft), wdLineStyleSingle, wdLineWidth150pt, wdColor:=wdColorBlue
DefineBorder .Borders(wdBorderRight), wdLineStyleSingle, wdLineWidth150pt, wdColorBlue
DefineBorder .Borders(wdBorderTop), wdLineStyleSingle, wdLineWidth150pt, wdColorBlue
DefineBorder .Borders(wdBorderBottom), wdLineStyleSingle, wdLineWidth150pt, wdColorBlue
DefineBorder .Borders(wdBorderVertical), wdLineStyleSingle, wdLineWidth150pt, wdColorBlue
End With
With oTS.Condition(wdLastRow) 'Associated with Total Row Checkbox
.Shading.BackgroundPatternColor = wdColorRose
DefineBorder .Borders(wdBorderLeft), wdLineStyleSingle, wdLineWidth150pt, wdColorRed
DefineBorder .Borders(wdBorderRight), wdLineStyleSingle, wdLineWidth150pt, wdColorRed
DefineBorder .Borders(wdBorderTop), wdLineStyleSingle, wdLineWidth150pt, wdColorRed
DefineBorder .Borders(wdBorderBottom), wdLineStyleSingle, wdLineWidth150pt, wdColorRed
DefineBorder .Borders(wdBorderVertical), wdLineStyleSingle, wdLineWidth150pt, wdColorRed
End With
ActiveDocument.Tables.Add Selection.Range, 5, 7
Selection.Tables(1).Style = "Table with Just One Condtional Style Element"
lbl_Exit:
Exit Sub
End Sub
Sub DefineBorder(oBorder As Object, Optional LineStyle As WdLineStyle = wdLineStyleSingle, Optional LineWidth As WdLineWidth, _
Optional wdColor As wdColor = wdColorAutomatic)
On Error Resume Next
With oBorder
.LineStyle = LineStyle
.LineWidth = LineWidth
.Color = wdColor
End With
On Error GoTo 0
lbl_Exit:
Exit Sub
End Sub
TABLE STYLE OPTION CHECKBOXES
After the macro runs, there is a five row, seven column table inserted into the document with conditional formatting displayed only on the “Header Row.”
The reason for this is because when the table is inserted the Table Style Options on the Table Design tab are configured with “Header Row, Last Column, and Banded Rows” checked with “Total Row, Last Column and Banded Columns” unchecked.
When I then checked “Total Row,” my style defined last row formatting was displayed.
My style is formatted with only “Header Row and Total Row” formatting. Why was “First Column and Banded Rows” checked and “Total Row” not? I mean, if I go to the trouble of creating a style with conditional formatting, it seems to reason that I would want to see that formatting applied when I apply that style to a table. Why would I need to take the extra steps of selecting the table, clicking Table Design tab, and checking unchecked boxes?
While investigating, I read that:
"Set as Default" Function: If you modify a table (e.g., check "Total Row") and right-click to select "Set as Default," those specific check marks will appear on all future tables.
So, I selected the table, and checked “Header Row and Total” and unchecked the other four boxes, then selected the style in the Table Styles Gallery, right clicked and applied "Set as Default." I inserted another table, applied the style but still, the formatting was incorrect for the second table. I had to again, select it, select Table Design and check the unchecked box for Total Row.
I also read that the default configuration of the Table Style Option buttons is set as Header Row, First Column and Banded Rows checked whenever a new table is inserted in a document. That seems a bit illogical as I believe the default table style when a new table is inserted using Insert>Tables>Table is "Table Grid". The built-in Table Grid style doesn't have conditional formatting applied to either the Header Row, First Column or Banded Rows. One may wonder why are the boxes checked in that case? But they are.
However, that theory is debunked because if you insert a table using a simple VBA procedure:
Sub InsertSimpleTable()
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 8, 6)
MsgBox oTbl.Style.NameLocal
End Sub
... which again has no conditional formatting applied, the Table Style Option boxes checked are "Banded Rows" and "Banded Columns" (the Header Row and First Column boxes are unchecked). Where is the logic in that? What triggered this change?
It seems to me that the logical behavior would be that whenever a Conditional Formatting is defined in the table style that the associated Table Style Option button would be checked and the others unchecked when the table is inserted and that style applied. If a user then wants to then turn off the display of one or more conditions, then they uncheck it.
Scenario: I need to create a document with 100 tables with conditional formatting applied to the Header Row and Total row. First table contains 3 rows, the second 4 four rows, third 5 row … the one hundredth 102 rows. I create a style with conditional Header and Total row formatting. I insert the first table apply the style, select the table, click Table Design Tab, check Total Row in table design options. I shouldn’t have to repeat those ridiculous steps for each of the next 99 tables. Madness!!
TABLE STYLES IN GENERAL
Next is table styles in general. The Table Design>Table Gallery has a plethora (a hundred or more) table styles displayed. Shauna Kelly discusses this absurdity in her page on tables styles (can't seem to post link) and how to remove some or all of them.
She is right. Despite the artificial idiot solution to the prompt Word Table Design>Table Style Gallery remove a displayed style:
To remove a displayed table style from the gallery in Word, right-click the specific style in the Table Design tab's gallery and select Remove form Style Gallery. This instantly removes the style from view in the current document, although it remains available in the full style list.
There is no "Remove from Style Gallery" and as she states, there is no apparent way in the user interface to remove these built-in table table styles.
Unfortunately, her suggested VBA method for doing so (in which I think there are typos):
Sub HideATableStyleButMakeItVisibleWhenUsed()
With ActiveDocument.Styles(Word.wdStyleTableLightShading)
.Visibility = True 'Yes, True.
.UnhideWhenUsed = True
End With
End Sub
nor my altered version makes any difference.
Sub MyAlteredVersion()
With ActiveDocument.Styles(Word.wdStyleTableLightGrid)
.Visibility = False
.UnhideWhenUsed = False
.Hidden = True
End With
lbl_Exit:
Exit Sub
End Sub
At other places on the web, I have read that you can perhaps "hide" these styles with the Manage Styles "Restrict" dialog or Manage Styles "Recommended" Dialog. I applied restriction to all List Tables styles and still all 49 List Table type styles doggedly persist in the Table Style gallery.
Attempts to hide using the visibility settings with the Style Manager Recommended Tab also fails:
You can select Table Styles that are visible in the Table Style Pane and try to make them hidden but the associated visibility buttons are dimmed.
All of my testing has been done with Word 2019. Does anyone have any evidence that any of these oddities/anomalies are resolved in new versions?
Thank you.
1 Reply
This is, this issue happened in Word 2019 and later versions, including Word 2021 and Microsoft 365.
https://learn.microsoft.com/en-us/office/vba/api/word.conditionalstyle
https://learn.microsoft.com/en-us/office/vba/api/word.tablestyle.condition