How to use VBA to modify tables in Microsoft Word

Altering quite a few tables in a Phrase doc one after the other could be a tedious challenge. As an alternative, use VBA!

Microsoft Word icon on screen

Picture: PixelMe/Shutterstock

Microsoft Phrase tables are a robust function, and a few paperwork have many. When working with a number of tables, it is not unusual for somebody to come back in on the finish and say, “Can you modify the tables in order that they’re all … ?” 

SEE: 83 Excel ideas each person ought to grasp (TechRepublic)

You probably have a whole lot of tables and somebody asks for lots of formatting adjustments, you would possibly panic: however do not. Utilizing VBA’s Tables assortment, you may cycle by all of the tables in a doc and make the identical change(s). On this article, I am going to present you two easy VBA procedures that cycle by the Tables assortment. The primary one converts every desk to textual content. The second, adjustments the border colour to blue.

I am utilizing Microsoft 365 on a Home windows 10 64-bit system, however you should utilize earlier variations. VBA procedures aren’t supported by Phrase On-line. In your comfort, you may obtain the demonstration .docm, .doc and .cls information.

Changing to textual content

A Phrase desk is like another desk; it shows rows and columns of associated knowledge. Creating and formatting one is easy, nevertheless it’s simply as straightforward to finish up with inconsistencies from desk to desk when there are a whole lot of tables in a posh doc. It takes much more time to reign in all these inconsistencies than it does to create them within the first place.  

The process in Itemizing A is an easy For Every loop that cycles by all of the tables within the present doc. To take action, the code references the Tables assortment and the Separator desk property’s wdSeparateByTabs fixed. This can be very easy and does solely this one factor: converts all tables to textual content. You might be changing all tables—that is essential to notice since you would possibly convert a desk you do not need transformed. This process will not allow you to choose and select.

Itemizing A

Sub ConvertTblsToText()

    ‘Convert all tables to textual content.

    Dim tbl As desk

    If ActiveDocument.tables.Rely = 0 Then

        MsgBox “There are not any tables on this doc.”, vbOKOnly, “Error”

        Exit Sub

    Finish If

    For Every tbl In ActiveDocument.tables

        ‘wdSeparateByCommas, wdSeparateByDefaultListSeparator,

        ‘wdSeparateByParagraphs, wdSeparateByTabs

        tbl.ConvertToText Separator:=wdSeparateByTabs

    Subsequent

Finish Sub

When changing a desk to textual content, you could have 4 delimiters to think about:

  • wdSeparateByCommas
  • wdSeparateByDefaultListSeparator
  • wdSeparateByParagraphs
  • wdSeparateByTabs

I’ve added these constants to the code as a remark, so it will likely be simpler so that you can adapt this code to your individual work. Utilizing the Tables assortment, you possibly can achieve this a lot with the tables; I selected to transform as a result of it requires so little code. Nonetheless, inside that loop, you possibly can change a single property for all tables or utterly reformat all of them. The ability is within the loop that provides you entry to the Tables assortment. From there, the chances are quite a few.  

SEE: Workplace 365: A information for tech and enterprise leaders (free PDF) (TechRepublic)

To enter the process, press Alt + F11 to open the Visible Fundamental Editor. Within the Mission Explorer to the left, choose ThisDocument. You may enter the code manually or import the downloadable .cls file. As well as, the process is within the downloadable .docm, and .doc information. In the event you enter the code manually, do not paste from this internet web page. As an alternative, copy the code right into a textual content editor after which paste that code into the ThisDocument module. Doing so will take away any phantom internet characters that may in any other case trigger errors.

In case you are utilizing a ribbon model, you’ll want to save the workbook as a macro-enabled file. In the event you’re working within the menu model, you may skip this step. Now, let’s execute this process within the demonstration file proven in Determine A as follows:

  1. Click on the Developer tab after which select Macros within the Code group.
  2. Within the ensuing dialog, select ConvertTblsToText and click on Run.

Determine A

wordconverttable-a.jpg

  Execute this macro to transform all tables to textual content.

After operating this process, all three tables at the moment are plain textual content strings, separated by tabs, as proven in Determine B. Bear in mind, in case your delimiter is not tab characters, you’ll want to replace that property within the code. As well as, if the doc has no tables, the code will show an data message field after which cease. You will wish to add a extra descriptive message to your message field more than likely.

Determine B

wordconverttable-b.jpg

  The tables at the moment are plain textual content. 

If you wish to proceed and also you’re utilizing the demonstration file, press Ctrl + Z 3 times to undo the desk conversions. Or shut the file with out saving and reopen. We’re about to alter desk properties by increasing the process a bit.

Altering a format

Itemizing A cycles by the Tables assortment, however you are able to do far more than convert the tables to textual content. You may apply a brand new desk type, change a border colour and so forth. We’ll preserve this subsequent process, Itemizing B, so simple as the primary by altering just one property, the surface border colour.

Itemizing B

Sub ChangeTableBorderColor()

    ‘Change outdoors border colour to blue.

    Dim tbl As desk

    If ActiveDocument.tables.Rely = 0 Then

        MsgBox “There are not any tables on this doc.”, vbOKOnly, “Error”

        Exit Sub

    Finish If

    For Every tbl In ActiveDocument.tables

        tbl.Borders.OutsideColor = wdColorBlue

    Subsequent

Finish Sub

This process additionally cycles by the Tables assortment, stopping at every desk within the doc and altering its outdoors border colour to blue, as proven in Determine C. I selected this property as a result of there are such a lot of potentialities (and colour constants). However as soon as you understand how to cycle by the Tables assortment, it is simple to make elaborate adjustments mechanically through the use of VBA.

Determine C

wordconverttable-c.jpg

The borders are blue now. 

There is a little bit of error-handling in each procedures, however you may want extra. As well as, it is unlikely that you will wish to work by all these steps each time you wish to run the process. As an alternative, add the macro to the Fast Entry Toolbar. To take action, learn Easy methods to add Workplace macros to the QAT toolbar for fast entry.

Additionally see

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox