How to modify tables in Microsoft Word using VBA – Guide

Changing individual tables in a Word document would be a time-consuming task. Instead, use VBA! Tables are a powerful tool in Microsoft Word and some documents include many of them. When working with a large number of tables, it’s not uncommon for someone to ask, “Can you change the tables so they’re all…?” towards the end. You can panic if you have a lot of tables and someone requests a lot of formatting changes, but doesn’t. You can cycle through all tables in a document using VBA’s Table collection and perform the same adjustment(s). I’m going to show you two basic VBA functions that loop through the Tables collection in this article. Each table is converted to text in the first step. The second option is to make the border blue. I’m running Microsoft 365 on a 64-bit Windows 10 PC, although earlier versions are also supported. Word Online does not support VBA procedures. You can download demonstration.docm, .doc and.cls files for your convenience.

converting to text

A Word table is like any other table; it displays rows and columns of related data. Creating and formatting one is simple, but it’s just as easy to finish. up with table-to-table inconsistencies when there are many tables in a complex document. It takes a lot longer to reign in all these inconsistencies than it does to create them in the first place. The procedure in Listing A is a simple For Each loop that loops through all the tables in the current document. To do this, the code references the Tables collection and the wdSeparateByTabs constant of the Separator table property. It’s extremely simple and does just one thing: converts all tables to text. You are converting all tables – this is important to note because you can convert a table that you don’t want to convert. This procedure will not allow you to select and choose. Listing A Sub ConvertTblsToText() ‘Convert all tables to text. Dim tbl as table If ActiveDocument.tables.Count = 0 Then MsgBox “There are no tables in this document.”, VbOKOnly, “Error” Exit Sub End if For each tbl in ActiveDocument.tables ‘wdSeparateByCommas, wdSeparateByDefaultListSeparator, ‘wdSeparateByParagraphs, wdSeparateByTabs tbl.ConvertToText separator := wdSeparateByTabs Next End Sub When converting a table to text, you have four delimiters to consider: I have added these constants to the code as a comment so it will be easier for you to adapt this code to your own work. Using the Tables collection, you could do a lot with tables; I chose convert because it requires so little code. However, within that loop, you can change a single property for all tables or completely reformat all of them. The power is in the loop that gives access to the Tables collection. From there, the possibilities are endless. To enter the procedure, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer on the left, select ThisDocument. You can either enter the code manually or import the .cls file for download. Also, the procedure is in the .docm and .doc files for download. If you enter the code manually, please do not paste from this web page. Instead, copy the code into a text editor and paste it into the ThisDocument module. This will remove all ghost characters from the web that would otherwise cause errors. If you are using a ribbon version, be sure to save the workbook as a macro-enabled file. If you are working on the menu version, you can skip this step. Now, let’s perform this procedure on the demo file as follows: After performing this procedure, all three tables are now plain text strings, separated by tabs. Remember, if your delimiter isn’t tab characters, be sure to update this property in your code. Also, if the document does not have tables, the code will display an informational message box and then stop. You will likely want to add a more descriptive message to your inbox. If you want to continue and are using the demo file, press Ctrl + Z three times to undo table conversions. Or close the file without saving and reopen it. We are about to change the properties of the table, expanding the procedure a bit.

Changing a format

Listing A walks through the Tables collection, but you can do much more than convert the tables to text. You can apply a new table style, change the border color, and so on. We’ll keep this next procedure, Listing B, as simple as the first, changing only one property, the outer border color. Listing B Sub ChangeTableBorderColor() ‘Change the outer edge color to blue. Dim tbl as table If ActiveDocument.tables.Count = 0 Then MsgBox “There are no tables in this document.”, VbOKOnly, “Error” Exit Sub End if For each tbl in ActiveDocument.tables tbl.Borders.OutsideColor = wdColorBlue Next End Sub This procedure also cycles through the Tables collection, stopping at each table in the document and changing its outer border color to blue. I chose this property because there are so many possibilities (and color constants). But when you know how to Scrolling through the collection of tables, it’s easy to make changes that are made automatically using VBA.

Final note

I hope you like the guide How to modify tables in Microsoft Word using VBA. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

How to modify tables in Microsoft Word using VBA  2022  - 98How to modify tables in Microsoft Word using VBA  2022  - 91How to modify tables in Microsoft Word using VBA  2022  - 67How to modify tables in Microsoft Word using VBA  2022  - 92