How to use style or VBA to assign only desired appearance to all tables in Word
The article, How to Control the Horizontal Alignment of a Word Table, shows you how to save a formatted table as a quick table in Microsoft Word. Then you can insert the array shell, format it and start entering data immediately. A quick table can save you a lot of time. But using this feature isn’t the only way to quickly shape tables.
SEE: 83 Excel tricks every user should master (TechRepublic)
In this article, I will show you how to apply the same style to all tables in the document one by one. I’ll also show you a VBA procedure that will do the same thing, but much faster and with little effort on your part.
I am using Microsoft 365 on a Windows 10 64 bit system. (I recommend that you do not upgrade to Windows 11 until all issues are resolved.) Word Online does not support any of the options described in this article. For your convenience, you can download the .docm, .doc and .cls demo files. If you’re working with your own files, you’ll need to know how to insert and edit tables, but I’ll provide plenty of basic instructions.
How to Style a Table in Word
It doesn’t matter how it turns out, but often you end up with a few tables with different formats. You can retrieve data online or receive tables from collaborators. Regardless of how it happens, you probably want all tables to look the same.
If you have a document with only a few incompatible tables, combine Word’s Find feature with the table style of your choice. You’ll jump from table to table, but it won’t take long because Find will find the tables for you, skipping all the rest of the content. You can use a built-in or custom table style. Both will be available in the painting gallery.
Now for a quick example. The short document presented in Figure A has three tables. The formats and alignment are different for all three. Using Find, you will select and format, one by one. So it’s not a good option for a big document with a lot of tables.
To format the three tables using a style, do the following:
- On the Home tab, click Edit and choose Search. This will open the navigation pane.
- Click the Find document drop-down menu (next to the magnifying glass icon) and select Tables (Figure B).
- This will display two new icons in the pane for moving forward and backward through the document.
- Click the down arrow and Word will find the following table in the document (Figure C). Don’t be surprised if you have to click this button more than once, but you can see selection handles when a table is selected, so use that as a guide.
- With a table selected, click the Table Design contextual tab and choose a style from the Table Styles gallery. I chose List Table List Colorful (the green one). I simply clicked on one in the gallery; it doesn’t matter which style you use for our purposes.
- After applying the style, click the down arrow again to select the next table and apply the same style from the gallery.
Repeat the last step one more time, and you’re done, as you can see in Figure D. If you are working with your own file, you may need to repeat this last step several times.
It was quick and easy, if you only have a few tables. But oops…. I applied a similar style to the second table by accident! In such a simple document, you can fix this problem quickly, but it’s a potential problem, so be careful.
If you have different wrapping or alignment requirements, you can create custom tables and apply the custom styling in the same way. If you have a lot of mismatched tables, a VBA procedure will be faster and avoid errors. Before moving on, you’ll want to close the demo file without saving or click Ctrl + Z three times to distort the tables so they don’t match the next section.
The VBA procedure
Formatting tables using VBA is simple: very little code is required. Knowing which properties to modify is the key to getting it right. In our case we want to apply the same style we used in the last section, “List Table 6 Colorful” to all tables in a document and this is what List A Is.
'Apply built-in style to all tables in the document.
Dim tbl As Table
On Error GoTo ErrHandler
For Each tbl In ActiveDocument.Tables
tbl.Style = "List Table 6 Colorful - Accent 6"
MsgBox "Error" & Err.Number & " " & Err.Description
The procedure defines a table object and uses the For Each statement to search through all the tables in the document, similar to the manual search function. the
tbl.Style = "List Table 6 Colorful - Accent 6" The statement then applies this style to each table.
The style name must be fully qualified; you can find it by hovering over the thumbnail in the gallery. Although I haven’t done it yet, you can add other formats to the For Each statement. After typing
tbl. VBA will display a list of possible properties and events. Error handling is basic and in such a simple procedure it should be adequate, but be prepared to do some extensive testing before making this decision.
If you’re using a ribbon version, be sure to save the workbook as a macro-enabled file. If you are working in the menu version, you can skip this step.
To enter the procedure, press Alt + F11 to open the Visual Basic Editor (VBE). In the Project Explorer on the left, select ThisDocument. You can enter the code manually or import the downloadable .cls file. Additionally, the macro is found in downloadable .docm, .doc, and .cls files. If you enter the code manually, do not paste from this webpage. Instead, copy the code into a text editor, and then paste that code into the ThisDocument module. This will remove any ghost web characters that might otherwise cause errors.
To run the code, return to the document and click the Developer tab. In the Macros group, click Macros. Select ApplyListTable6Colorful from the dialog box shown in Figure E, then click Run. The results are instantaneous.
You’re unlikely to want to follow this routine every time, so consider adding the procedure to the Quick Access Toolbar (QAT) or a custom group on the ribbon. For instructions, read How to Add Office Macros to the QAT Toolbar for Quick Access.