Dennis has two custom table styles that he's created. One style (ShortTable) is used for shorter tables and the other (LongTable) is for longer tables. Dennis wonders if there is a way to step through each table in a document and automatically apply the correct style for each table. If a table has 7 or fewer body rows it should use the ShortTable style and longer tables should use the LongTable style.
The easiest way to accomplish this task is to use a macro. The following will do the trick, as it steps through each table in a document and applies the appropriate table style:
Sub ApplyTableStyles()
Dim tbl As Table
Dim bodyRows As Long
Dim styShort As Style
Dim styLong As Style
Dim sMsg As String
On Error Resume Next
Set styShort = ActiveDocument.Styles("ShortTable")
Set styLong = ActiveDocument.Styles("LongTable")
On Error GoTo 0
sMsg = ""
If styShort Is Nothing Then
sMsg = "Style ShortTable does not exist."
Else
If styShort.Type <> wdStyleTypeTable Then
sMsg = "Style ShortTable is not a table style."
End If
End If
If styLong Is Nothing Then
If sMsg > "" Then sMsg = sMsg & vbCr
sMsg = sMsg & "Style LongTable does not exist."
Else
If styLong.Type <> wdStyleTypeTable Then
If sMsg > "" Then sMsg = sMsg & vbCr
sMsg = sMsg & "Style LongTable is not a table style."
End If
End If
If sMsg > "" Then
MsgBox sMsg, vbExclamation
Exit Sub
End If
For Each tbl In ActiveDocument.Tables
bodyRows = tbl.Rows.Count
' Adjust row count if first row is a heading row
If tbl.Rows(1).HeadingFormat = True Then bodyRows = bodyRows - 1
If bodyRows <= 7 Then
tbl.Style = styShort
Else
tbl.Style = styLong
End If
Next tbl
End Sub
Note that at the very beginning of the macro the two table styles are set into the styShort and styLong variables. This is done because if either style is set to nothing, it means that the style doesn't exist. (It does no good to step through the tables and try to apply non-existent styles.)
In addition, both styShort and styLong are checked to ensure that they are actually table styles, as Word will generate an error if you try to apply a different type of style (such as a paragraph style or a character style) to a table.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (8522) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!
Word allows you to format table rows so that they don't split across a page break. If you have many tables you need to ...
Discover MoreWhen you get a text file from a program other than Word, tabular information may be formatted with nothing but spaces in ...
Discover MoreNeed a block of text to appear in a table? Word can do the conversion for you very quickly.
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments