Home » How to use a style or VBA to assign just the look you want for all tables in Word

How to use a style or VBA to assign just the look you want for all tables in Word

by News Times USA


Microsoft Word on screen
Image: justplay1412/Shutterstock

The article, How to control a Word table’s horizontal alignment, shows you how to save a formatted table as a Quick Table in Microsoft Word. Then, you can insert the table shell, formatting it and start entering data right away. A Quick Table can save you a lot of time. But using that feature isn’t the only way to quickly whip tables into shape.

SEE: 83 Excel tips every user should master (TechRepublic)

In this article, I’ll show you how to apply the same style to all the tables in the document, one by one. I’ll also show you a VBA procedure that will do the same thing, but much quicker and with little effort on your part.

I’m using Microsoft 365 on a Windows 10 64-bit system. (I recommend that you not upgrade to Windows 11 until all the kinks are worked out.) Word Online doesn’t support either of the options discussed in this article. For your convenience, you can download the demonstration .docm, .doc and .cls files. If you’re working with your own files, you’ll need to know how to insert and modify tables, but I’ll supply a lot of basic instructions.

How to apply a style to a table in Word

It doesn’t matter how it happens, but often, you end up with a few tables with different formats. You might be grabbing data from online or receiving tables from collaborators. Regardless of how it happens, you’ll probably want all tables to look the same.

If you have a document, with only a few mismatched tables, combine Word’s Find feature with the table style of choice. You’ll move from table to table, but it won’t take long because Find will find the tables for you, skipping all the other content. You can use built-in or custom table style. Both will be available in the table gallery.

Now, let’s work through a quick example. The short document shown in Figure A has three tables. The formats and alignment are different for all three. Using Find, you’ll select and format, one by one. So, it’s not a great option for a large document with lots of tables.

Figure A

Let’s apply the same style to all three tables.

To format all three tables using a style, do the following:

  1. On the Home tab, click Editing and choose Find. Doing so will open the Navigation pane.
  2. Click the Search Document dropdown (next to the magnifying glass icon) and select Tables (Figure B).
  3. Doing so will display two new icons in the pane for moving forward and backward through the document.
  4. Click the down arrow, and Word will find the next table in the document (Figure C). Don’t be surprised if you have to click that button more than once, but you can see the selection handles when a table is selected, so use that as your guide.
  5. With a table selected, click the contextual Table Design tab and choose a style from the Table Styles gallery. I chose List Table List Colorful (the green one). I simply clicked one in the gallery; it doesn’t matter which style you use for our purposes.
  6. 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’re working with your own file, you might need to repeat that last step more than once.

Figure B

Use the Navigation pane to find tables.

Figure C

Click the down arrow button to select the next table; click the up arrow to find the previous table.

Figure D

A few clicks reformats all three tables.

That was quick and easy—if you have only a few tables. But oops …. I applied a similar style to the second table by accident! In such a simple document, you can quickly fix that problem, but it is a potential problem, so be careful.

If you have different wrapping or alignment requirements, you can create custom tables and apply the custom style the same way. If you have a lot of mismatched tables, a VBA procedure will be quicker and prevent errors. Before we move on though, you’ll want to close the demonstration file without saving or click Ctrl+Z three times to unformat the tables so they’re mismatched for the next section.

The VBA procedure

Formatting tables using VBA is easy—very little code is required. Knowing which properties to change 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 the tables in a document and that’s what Listing A does.

Listing A

Sub ApplyListTable6Colorful()

'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"

Next

Exit Sub

 

ErrHandler:

MsgBox "Error" & Err.Number & " " & Err.Description

End Sub

The procedure sets a table object and uses the For Each statement to cycle through all the tables in the document, similarly to the way the manual Find feature does. The tbl.Style = "List Table 6 Colorful - Accent 6" statement then applies that style to each table.

The style name must be complete; you can find it by hovering over the thumbnail in the gallery. Although I’ve not done so, you can add more formats to the For Each statement. After typing tbl. VBA will display a list of possible properties and events. The error-handling is basic, and in such a simple procedure it should be adequate but be prepared to test thoroughly before making that decision.

If you are using a ribbon version, be sure to save the workbook as a macro-enabled file. If you’re 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 to the left, select ThisDocument. You can enter the code manually or import the downloadable .cls file. In addition, the macro is in the downloadable .docm, .doc and .cls files. If you enter the code manually, don’t paste from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing so will remove any phantom 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 shown in Figure E and then click Run. The results are instant.

Figure E

Run the VBA procedure.

It’s unlikely that you’ll want to go through that routine every time though, 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.



Source link

Related Articles

Leave a Comment