Tue. Oct 26th, 2021


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

Microsoft Word icon on screen

Picture: PixelMe/Shutterstock

Microsoft Phrase tables are a strong function, and a few paperwork have many. When working with a lot of tables, it isn’t 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 suggestions each person ought to grasp (TechRepublic)

If in case you have quite a lot of tables and somebody asks for lots of formatting modifications, you may panic: however do not. Utilizing VBA’s Tables assortment, you’ll be able to cycle by means of 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 means of the Tables assortment. The primary one converts every desk to textual content. The second, modifications the border coloration to blue.

I am utilizing Microsoft 365 on a Home windows 10 64-bit system, however you should use earlier variations. VBA procedures aren’t supported by Phrase On-line. To your comfort, you’ll be able to 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, however it’s simply as straightforward to finish up with inconsistencies from desk to desk when there are quite a lot of tables in a fancy 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 means of 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 may convert a desk you don’t 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 have got 4 delimiters to contemplate:

  • wdSeparateByCommas
  • wdSeparateByDefaultListSeparator
  • wdSeparateByParagraphs
  • wdSeparateByTabs

I’ve added these constants to the code as a remark, so it is going to be simpler so that you can adapt this code to your individual work. Utilizing the Tables assortment, you can accomplish that a lot with the tables; I selected to transform as a result of it requires so little code. Nevertheless, inside that loop, you can change a single property for all tables or utterly reformat all of them. The ability is within the loop that offers 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 Venture Explorer to the left, choose ThisDocument. You’ll be able to enter the code manually or import the downloadable .cls file. As well as, the process is within the downloadable .docm, and .doc information. In case you enter the code manually, do not paste from this internet web page. As a substitute, 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 should definitely save the workbook as a macro-enabled file. In case you’re working within the menu model, you’ll be able to 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 working this process, all three tables at the moment are plain textual content strings, separated by tabs, as proven in Determine B. Keep in mind, in case your delimiter is not tab characters, you should definitely replace that property within the code. As well as, if the doc has no tables, the code will show an info message field after which cease. You may need to add a extra descriptive message to your message field most certainly.

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 thrice 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 means of the Tables assortment, however you are able to do way more than convert the tables to textual content. You’ll be able to apply a brand new desk fashion, change a border coloration and so forth. We’ll hold this subsequent process, Itemizing B, so simple as the primary by altering just one property, the surface border coloration.

Itemizing B

Sub ChangeTableBorderColor()

    ‘Change exterior border coloration 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 means of the Tables assortment, stopping at every desk within the doc and altering its exterior border coloration to blue, as proven in Determine C. I selected this property as a result of there are such a lot of prospects (and coloration constants). However as soon as you understand how to cycle by means of the Tables assortment, it is easy to make elaborate modifications routinely 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’re going to need to work by means of all these steps each time you need to run the process. As a substitute, add the macro to the Fast Entry Toolbar. To take action, learn Methods to add Workplace macros to the QAT toolbar for fast entry.

Additionally see



Source link

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *