Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Table Style Observations, Bugs and Anomalies
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

This Microsoft Word Tips & Microsoft Word Help page discusses Table Styles. While it contains information and techniques relevant to any Word user, the target audience is the Word developer as the content focuses primarily on using table styles with VBA.

Introduction

The discussion that follows was inspired by my own struggle coming to grips with table styles and the excellent work and invaluable contributions of the renowned former Word MVP and late Shauna Kelly.  Shauna covered this topic extensively in her series of articles at Table styles.  While closely related, the content here is written in my own style, using my own terms.  The content may diverge from, omit, or where observations warrant even contradict Shauna's published content. This is in no way intended to disparage her work.

Shauna's articles outline the observations, bugs and anomalies that she observed when working with earlier versions of Word.  Some things have changed since those articles were last updated. My objective, is to provide VBA procedures that replicate her tests and compare her observations with the results I observe now using Word 2019.  This content should be considered a companion to her work, and I encourage each person reading this content to also read and consider hers.

Table Style

A Word Table Style is a reusable collection of formatting rules that you can apply to individual tables with a single click.  Components include:

Testing & Demonstration Documents

For the discussion that follows, you can download the companion Testing and Demonstration Documents.  The document set includes:

Observations

General

Table styles were introduced with Word 2002, well after the more familiar paragraph and character styles.  I feel confident, due to limited resources and to reduce costs, that Microsoft made every effort to "mesh" the new table style object into the existing fully developed style object.  For me, that goes a long way in accepting some (not all) of the anomalies and bugs still present in table styles as discussed below.

Complicating matters further, is the default "empty" Normal paragraph style and the the hierarchy of how Word applies formatting to document content using the document defaults (font and paragraph), table styles, then paragraph styles in that order.  Without a proper understanding of the default Normal style and this hierarchy, and despite formatting explicitly defined in a table style, the content actually displayed can be heavily and adversely affected by the physical table's overlaying paragraph style (the style applied at the table insertion point or applied directly to a table range).

While researching and testing to develop this tips page, I discovered the following general behaviors and formulated general practices. Understanding and following these can minimize both errors and frustration when working with table styles:

Site Note IconSee: For more on style hierarchy and the "empty" Normal style, see: Behind the Curtain: Styles Order of Operations  and How do Document Defaults relate to the Normal Style?.

Preventing Tables/Rows Break (Splitting) Across Pages

See Shauna's 4 ways to control page breaks within tables and table styles (for developers)
1. In a Table Style, Prevent or Allow Page Breaks within Table Rows (.AllowBreaksAcrossPage)

Defines the base underlying behavior of all rows in a table with the style applied.

2. In a table: prevent or allow page breaks within specified rows (.AllowBreakAcrossPages)
3. In a table or a table style: keep the whole table on one page (.AllowPageBreaks)
4. .KeepWithNext

Condition

Code
(assumes oStyle is a Word table style)

Effect
Note: To see the described effect, the associated Table Style Options checkboxes must be checked.


First row

oStyle.Table.Condition(wdFirstRow) _
.ParagraphFormat.KeepWithNext = True

The first row will be displayed on the same page as the first paragraph of the second row

Last row

oStyle.Table.Condition(wdLastRow) _
.ParagraphFormat.KeepWithNext = True

The last row will be displayed on the same page as the first paragraph following the table. Can be used to keep the last row of a table on the same page as its following caption.

First column

oStyle.Table.Condition(wdFirstColumn) _
.ParagraphFormat.KeepWithNext = True

Word will (attempt to) keep all rows on the same page as the following paragraph. In a qualified table, this has the effect of keeping the whole table and the paragraph immediately following the table on one page. If the table is too large to fit on one page, then the table will spill over onto second and if necessary subsequent pages. This has several consequences:

If the paragraph following the table is a caption, this has the side effect of keeping the table with its caption (unless you're in the unhappy position where the table itself would fit on one page, but the table plus its caption is too tall; in that case, the caption will be left alone on the second page).

If the paragraph following the table is not a caption, then you may see awkward pagination as Word tries to keep too much together on one page.

Last column

oStyle.Table.Condition(wdLastColumn) _
.ParagraphFormat.KeepWithNext = True

Can be set to True (returns -1) or False (returns 0). No apparent effect.

Top left cell

oStyle.Table.Condition(wdNWCell) _
.ParagraphFormat.KeepWithNext = True

The first row will be displayed on the same page as the first paragraph of the second row. That is, this has the same effect as setting KeepWithNext on the first row.

Top right cell

oStyle.Table.Condition(wdNECell) _
.ParagraphFormat.KeepWithNext = True

Can be set to True (returns -1) or False (returns 0). No apparent effect.

Bottom left cell

oStyle.Table.Condition(wdSWCell) _
.ParagraphFormat.KeepWithNext = True

The first paragraph of the last row will be displayed on the same page as first paragraph following the table. That is, this has the same effect as setting KeepWithNext on the last row.

Bottom right cell

oStyle.Table.Condition(wdSECell) _
.ParagraphFormat.KeepWithNext = True

Can be set to True (returns -1) or False (returns 0). No apparent effect.

Column and row banding

oStyle.Table.Condition(wdEvenRowBanding)_
.ParagraphFormat.KeepWithNext = True

oStyle.Table.Condition(wdEvenColumnBanding)_
.ParagraphFormat.KeepWithNext = True

oStyle.Table.Condition(wdOddRowBanding) _
ParagraphFormat.KeepWithNext = True

oStyle.Table.Condition(wdOddColumnBanding) _
.ParagraphFormat.KeepWithNext = True

Can be set to True (returns -1) or False (returns 0). No apparent effect.

Since when we apply a True or False condition to a property, we expect some effect, I agree with Shauna's classification of "No apparent effect" on these conditions as little bugs.  However, since any effect of Last column, Top right cell or Bottom right cell would be identical to the effect of the corresponding First column, Top left cell or Bottom left cell it is rather moot.  As for the column and row banding conditions, well ???

Borders in Table Styles

See Shauna's: Borders in Table Styles
1. Style.Borders

Site Note IconTable styles, unlike paragraph styles, have a functional .Table property.  It is the .Table property .Borders property that should be used.

2. Style.Table.Borders

Applies to all borders in a table with the style applied.

3. Style.ParagraphFormat.Borders

Applies to borders around paragraphs of text within table cells. See: modTblBorders.Observation_6

4. Style.Font.Borders

Applies to border applied around text in a table cell. See: modTblBorders.Observation_7

5. Style.Table.Condition(n).Borders

Applies to borders applied around or in cells of the condition region. See: modTblBorders.Observation_8

6. Style.Table.Condition(n).ParagraphFormat.Borders

Applies to borders applied around paragraphs in cells of the condition region.  See: modTblBorders.Observation_9

7. Style.Table.Condition(n).Font.Borders

Applies to borders applied around text in cells of the condition region.  I can't see where I would ever use this property so I have no issue with Shauna's advice to avoid it.  However, my observations differ from hers. See: modTblBorders.Observation_10

 Built-in Table Styles Use and Display

See Shauna's: How to hide table styles on the Table Tools Design tab in Word (for developers)

Shauna addressed the insanity, back then, of having 99 different table styles in the Table Design Table Style Gallery and provided two simple macro procedures to hide individual Built-In Table Styles.  Here are Shauna's macros (slightly modified to suit my style):

VBA Script:
Sub HideNamedTableStyle_MakeItVisibleWhenUsed()
  With ActiveDocument.Styles("List Table 1 Light - Accent 1") 'Substitute any table style name.
    .Visibility = True 'Counter intuitive
    .UnhideWhenUsed = True 'Intuitive
  End With
lbl_Exit:
  Exit Sub
End Sub

Sub HideNamedTableStyle_KeepHiddenEvenIfUsed()
  With ActiveDocument.Styles("List Table 1 Light - Accent 1") 'Substitute any table style name.
    .Visibility = True 'Counter intuitive
    .UnhideWhenUsed = False 'Intuitive
  End With
lbl_Exit:
  Exit Sub
End Sub

With Word 2019, using a new clean Normal template, the Table Design Table Gallery displays 105 built-in table styles in three categories:

The internet is now replete with AI generated solutions to remove one or more of these built-in table styles. Most simply to suggest selecting the style in the gallery and selecting "Remove from Style Gallery."  That works for paragraph styles in the Home Table Style Gallery, but unfortunately there is no "Remove from Style Gallery" control provided for table styles.

There is a "Delete Table Style," but that simply deletes the styles formatting from the table with the selected style applied.

tables_styles_1

Others suggest hiding the built-in styles using the Manage Styles dialog Recommend tab. Again, this suggestion fails because the "Show", "Hide" and "Hide Until Used" commands in that dialog are always disabled (dimmed) for any table style selected.

tables_styles_2

Site Note IconAs it seems the features and functions of the Recommend dialog list are not applicable to table styles, it begs the question "Why are table styles in this list?"

Since they "are" listed, we know that in addition to the 105 table styles already cluttering up the Table Style Gallery, there are an additional 142 hidden from view until used:

Without the foray into the Style Manager dialog, the average Word may never have known these additional styles existed or if they did, how to apply them.

How are they applied for the first timee?  Since the hidden (or Hidden until Used) styles are not shown in either the Table Style Gallery (or, if you have it displayed, the old toolbar "Classic Style Gallery"), ... 

table_styles_3

... the only apparent way is with with code.  You can simply apply the hidden style to a selected table:

VBA Script:
Sub ApplyHiddenTableStyle
  'Select the table before running this code
  Selection.Tables(1).Style = "Table Classic 1"
lbl_Exit:
  Exit Sub
End Sub

Or first make the style visible, then apply it to a table with the user interface:

VBA Script:
Sub UnhideNamedTableStyle()
  'Substitute any table style name
  ActiveDocument.Styles("Table Classic 1").Visibility = False 'Counter intuitive
lbl_Exit:
  Exit Sub
End Sub

Once the style is used it is unhidden, it will appear under the Plain table category of the Table Style Gallery and  if displayed, the old toolbar Classic Style Gallery.

table_styles_4

Now we have 106 built-in table styles appearing in the Table Style Gallery.  We could remove the ones we don't want one by one using Shauna's simple macros, or use more complex VBA procedures to manage the styles displayed. In the companion Test Document for this tips page, I have included VBA procedures to:

You can experiment with these procedures using the MacroButton fields in the Test Document.

table_styles_5
Illustrates the MacroButton fields available in the Test Document

Reading Conditions of a Word table style (for developers)

See Shauna's: Reading Conditions of a Word table style (for developers)

Shauna reported getting spurious errors. I never once experienced the type of error she described in this article. However, I can't discount it and have included her workaround in each of the applicable procedures used in this document.

Shading  (or background coloring and texture) in Table Styles

See Shauna's: Shading (ie background colour) in table styles
1. Style.Shading

Site Note IconTable styles, unlike paragraph styles, have a "functional" .Table property.  It is the .Table property .Shading property that should be used.

2. Style.Table.Shading

Applies to cells in a table with the style applied. See: modTblShading.Observation_2

3. Style.Table.Condition(n).Shading

Applies to cells in a condition region of a table with the style applied.

4. Style.ParagraphFormat.Shading

Applies to background color, foreground color and texture applied to paragraphs within cells of tables with the table style applied. Contrary to Shauna's observations, this method can work with the following anomalies:

5. Style.Font.Shading

Applies to background color, foreground color and texture applied to text within cells of tables with the table style applied. Contrary to Shauna's observations, this method can work with the following anomalies:

6. Style.Condition(n).ParagraphFormat.Shading

Applies to background color, foreground color and texture applied to paragraphs within cells in a condition(n) region of of tables with the table style applied. Contrary to Shauna's observations, this method can work with the following anomalies:

7. Style.Condition(n).Font.Shading

Applies to the shading properties of text within cells in a condition(n) region of tables with the table style applied. Contrary to Shauna's observations regarding Word 2010, this method cannot apply the three crucial .Shading properties to text.
See: modTblShading.Observation_9

Giving Table Styles a Second Chance

See Shauna's:Why I don’t use Custom Table Styles in Microsoft Word 2002 and 2003

At this point, Shauna had thrown in the towel and given up on table styles. I do not intend to contradict anything she expressed in that article as it was based on her observations at the time using an older version of Word.

However, while admittedly table styles remain somewhat "half-baked," given that you understand the importance of the "empty" Normal style and Word's style hierarchy, I think there is some utility with table styles making them worthwhile

Site Note Icon The companion Test Document for this Tips Page contains several Macrobutton Fileds to allow you to visualize this behavior.

table_styles_6

Table styles are not perfect, but what in Word is? Personally, I can't image a case where I would need to know the returned value a border property or shading property. With that said, and in view of all my observations above, there is really only one issue unresolved, and I am for giving tables styles a second chance. That issue is the stubbornly contrary diagonal borders that can't be properly applied with code. In cases where diagonal borders are required, we will just have to finalize style with the user interface.

How to finalize a "Half-Baked Table Style"

The image below illustrates a table with a half-baked table style applied. The style is developed with VBA. Following the general observations listed above; the table's overlaying paragraph style is the "empty" Normal style.

table_styles_7

The following VBA code and comments describes how the table style and table where developed and displayed.

VBA Script:
Option Explicit
Private oTbl As Table
Private oStyle As Style
Private oTS As TableStyle
Private oCS As ConditionalStyle
Private strStyleDef As String
Sub CreateHalfBakedTableStyle()
  PrepForHalfBakedDemo
  'Apply font to overall table
  With oStyle
    .Font.Name = "Courier New"
    .Font.Size = 8
    .Font.ColorIndex = wdDarkRed
  End With
  'Apply condition font and paragraph attributes to first row
  With oTS
    'Apply table borders
    BorderDefintion .Borders(wdBorderLeft), wdLineStyleSingle, wdLineWidth100pt, wdColorRed
    BorderDefintion .Borders(wdBorderRight), wdLineStyleSingle, wdLineWidth100pt, wdColorRed
    BorderDefintion .Borders(wdBorderTop), wdLineStyleSingle, wdLineWidth100pt, wdColorRed
    BorderDefintion .Borders(wdBorderBottom), wdLineStyleSingle, wdLineWidth100pt, wdColorRed
    'For NWCell bottom and right border to display correctly, table vertical and horizontal borders
    'must be set to a small value.
    BorderDefintion .Borders(wdBorderVertical), wdLineStyleSingle, wdLineWidth050pt, wdColorRed
    BorderDefintion .Borders(wdBorderHorizontal), wdLineStyleSingle, wdLineWidth050pt, wdColorRed
     'Apply condition font and paragraph attributes to first row
    .Condition(wdFirstRow).Font.Name = "Arial"
    .Condition(wdFirstRow).Font.Size = "14"
    .Condition(wdFirstRow).Font.Bold = True
    .Condition(wdFirstRow).Font.ColorIndex = wdBlue
    .Condition(wdFirstRow).ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Condition(wdFirstRow).ParagraphFormat.SpaceAfter = 12
  End With
  'Format the NWCell
  Set oCS = oTS.Condition(wdNWCell)
  With oCS
    .Font.ColorIndex = wdWhite
    BorderDefintion .Borders(wdBorderLeft), wdLineStyleSingle, wdLineWidth100pt, wdColorBlue
    BorderDefintion .Borders(wdBorderRight), wdLineStyleSingle, wdLineWidth100pt, wdColorBlue
    BorderDefintion .Borders(wdBorderTop), wdLineStyleSingle, wdLineWidth100pt, wdColorBlue
    BorderDefintion .Borders(wdBorderBottom), wdLineStyleSingle, wdLineWidth100pt, wdColorBlue
  End With
  'You can set a DiagonalDown or a DiagonalUp border, but not both.
  'You can set a limited range of line styles. If you attempt to set an invalid line style, the line style reverts to wdLineStyleSingle
  'You can't set line width. Any attempt raises error. The line width displayed is determined by the current line width set in the Table Design Tab Borders Group or
  'the default line width 1 pt.
  'You can't set a color.  Any attempt results in the border disappearing.
  With oCS.Borders(wdBorderDiagonalDown)
    .LineStyle = wdLineStyleSingle
    On Error Resume Next
    .LineWidth = wdLineWidth075pt 'The line weight applied size applied will = the current width setting in the UI
    If Err.Number <> 0 Then Debug.Print Err.Number & " - " & Err.Description
    On Error GoTo 0
    'Color is fixed as black.  If you try to change, the border disappears.
    .Color = wdColorBrightGreen 'So in practice, don't use this line
    DoEvents
    'Restore the wiped line.
    .LineStyle = wdLineStyleSingle 'In practice, you wouldn't need this line.
  End With
  With oCS.Borders(wdBorderDiagonalUp)
    .LineStyle = wdLineStyleDashDotStroked 'This doesn't work. _
                                            Displays single line/width current width setting in UI.
    .LineStyle = wdLineStyleDashLargeGap 'This works, but width is reset to default line width 1 pt.
    Debug.Print .LineWidth 'Not correct. Always returns -1
    Debug.Print .LineStyle 'Not correct. Always returns 999999 (or undefined)
  End With
  oTbl.ApplyStyleHeadingRows = True
  oTbl.ApplyStyleFirstColumn = True
  'So at this point you only see the DiagonalUp border.  To finish, you must modify the style in the UI.
lbl_Exit:
  Exit Sub
End Sub
Sub PrepForHalfBakedDemo()
  On Error Resume Next
  ActiveDocument.Styles("HalfBakedTableStyle").Delete
  On Error GoTo 0
  For Each oTbl In ActiveDocument.Tables
    If oTbl.Title = "Half Baked Table Style Test" Then
      oTbl.Delete
      DoEvents
      Exit For
    End If
  Next
  Set oStyle = ActiveDocument.Styles.Add("HalfBakedTableStyle", wdStyleTypeTable)
  oStyle.BaseStyle = "Table Grid"
  Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 4, 8)
  oTbl.Style = "HalfBakedTableStyle"
  oTbl.Title = "Half Baked Table Style Test"
  Set oTS = oStyle.Table
lbl_Exit:
  Exit Sub
End Sub
Sub BorderDefintion(oBorder As Object, Optional LineStyle As WdLineStyle = wdLineStyleSingle, Optional LineWidth As WdLineWidth, _
                 Optional wdColor As wdColor = wdColorAutomatic, Optional Color As Long = -9999)
  On Error Resume Next
  With oBorder
    .LineStyle = LineStyle
    .LineWidth = LineWidth
    If Not Color = -9999 Then
      .Color = Color
    Else
      .Color = wdColor
    End If
  End With
  On Error GoTo 0
lbl_Exit:
  Exit Sub
End Sub

Due to the limitations (bugs) discussed previously and described in the VBA code, it is not possible to fully develop and display both a diagonal up and diagonal down border. We need to finalize this table style definition with the user interface.

Perform the following steps to finalize:

table_styles_8
table_styles_9

That's it. Your fully developed table style is now displayed in the gallery and ready to use.

Site Note IconSee: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

Site Note iconNote: This tips page, illustrations and examples were developed using Word 2019.

That's it! I hope you have found this tips page useful and informative.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo