VBA and tables in Word – tips on the Table.Uniform property

This article provides information about some of the issues you may run into when working with tables in Word via VBA in relation to the Table.Uniform property. You will also find help on how you can avoid issues with tables that are not uniform.

When working with Word tables in VBA, you may often want to perform actions on individual cells, rows, or columns. In that situation, the Table.Uniform property can be is useful:

  • If VBA returns .Uniform = True for a table, you can access individual rows or columns in that table without error.
  • On the other hand, if VBA returns .Uniform = False for a table, you may not be able to access individual rows or columns in that table without error. In that case, you may have to operate on the individual cells instead.

Unfortunately, VBA returns .Uniform = False in cases where it would be expected to return True.

According to Microsoft's description of the Table.Uniform property, the following applies:

  • True if all the rows in a table have the same number of columns. Read-only Boolean.

That description does not comply with the results you get when checking misc. tables. Table.Uniform returns False for some tables that live up to the criterion above.

Example of uniform Word table

Example of Word table that should return .Uniform = True. It has regular rows and columns without any visible deviations.   

Another example of Word table that should return .Uniform = True

Another example of Word table that should return .Uniform = True. In this case, the rows have different heights and the columns have different widths but the individual rows and columns are regular.  

You can type the VBA code below in the Immediate window in the Visual Basic Editior (VBE) to check whether the first table in the active document  is uniform. When you press Enter after the line, the result True or False will be shown below the line:

?ActiveDocument.Tables(1).Uniform

To check whether the first table in the selection is uniform, you can type:

?Selection.Tables(1).Uniform

Examples of tables for which Table.Uniform returns False even if they live up to Microsoft's criterion for uniform tables

Example 1: Table.Uniform returns False if one or more cells in a column do not have the same width as other cells in the same column

Example of table in which the vertical edges of columns do not align

Example of table in which the vertical edges of columns do not align, In this case, columns 1 and 2 have mixed cell widths.. That table lives up to the criterion for a uniform table since all rows in the table do have the same number of columns – in this case 3 columns. However Table.Uniform returns False.

NOTE: You may experience that a table that looks regular and without any mixed cell widths returns Table.Uniform = false. This may happen if e.g. the edge between a couple of cells has been adjusted. Even the slightest difference in cell widths in a column is sufficient to return Table.Uniform = false.

In case of a table like the one illustrated in the example above, it seems correct the Table.Uniform returns False even if it, according to the criterion, should return True. The table will result in an error if you attempt to access the individual columns.

Example 2: Table.Uniform may return False for a table that has been copied from Excel and pasted into Word

I have experienced that Table.Uniform consistently returns False for a table (without any merged cells) copied from Excel and pasted into Word if you select Keep Source Formatting as the paste option. This options will most often be selected automatically if you simply click Ctrl+V to paste. This is the case even if the table consists of a single cell only!

In this situation, it is incorrect that Table.Uniform returns False.

A table copied form Excel and pasted into Word using Keep Source Formatting returns Table.Uniform = False

A table copied form Excel and pasted into Word using Keep Source Formatting returns Table.Uniform = False

To make a table copied from Excel and pasted into Word act as unform, select the Paste option Merge Formatting instead.

If a copied Excel table (without any merged cells) already exists in the Word document, you can change it to act as uniform by applying any table styles via Ribbon > Table Design group > Table Style. To get access to the Table Design tab, the selection must be in the table.

You can also apply a table style via VBA. The following code line applies the table style "Table Normal" to the first table in the selection:

A table copied form Excel and pasted into Word using Merge Formatting returns Table.Uniform = True

A table copied form Excel and pasted into Word using Merge Formatting returns Table.Uniform = True

Selection.Tables(1).Style = ActiveDocument.Styles(wdStyleNormalTable)

VBA errors you may see when working with Word tables that are not uniform and how to avoid the errors

In case of a uniform table, you can access individual cells, rows, and columns via VBA without error. In case of a table that is not uniform, you need to act carefully to avoid run-time errors.

You cannot access individual rows via VBA if a ta table has vertically merged cells

If you try to access individual table rows via VBA in a table that has vertically merged cells, VBA will return run-time error 5991, "Cannot access individual rows in this collection because the table has vertically merged cells."

Runtime error 5991 occurs if VBA attempts to access individual rows in a table with vertically merged cells
Example of table with vertically merged cells

Example of table with vertically merged cells. Table.Uniform returns False even if each row can be said to have the same number of columns.

NOTE: Vertically merged cells will occur both if you merge vertically adjoining cells and if you split a cell into more rows.

In case of a table like the one illustrated in the example above, it seems correct the Table.Uniform returns False even if it, according to the criterion, should return True. The table will result in an error if you attempt to access the individual rows.

The VBA macro below works on all rows in all tables in the active document. Error 5991 occurs if a table has vertically merged cells.

Sub OperateOnAllRowsInAllTablesInActiveDocument()
    Dim oTable As Table
    Dim oRow As Row

    For Each oTable In ActiveDocument.Tables
        For Each oRow In oTable.Rows
            'Do your work
        Next oRow
    Next oTable
End Sub

You cannot access individual columns via VBA if a ta table has mixed cell widths

If you try to access individual table columns via VBA in a table that has mixed cell widths, VBA will return run-time error 5992, "Cannot access individual columns in this collection because the table has mixed cell widths."

Runtime error 5992 occurs if VBA attempts to access individual columns in a table with mixed cell widths

Mixed cell widths can occur if you have merged cells in adjoining columns or if the vertical edges of columns do not align. See the examples below.

Example of table with merged cells in adjoining columns

Example of table with merged cells in adjoining columns. That table does not live up to the criteria for a uniform table since all the rows in that table do not have the same number of columns. Table.Uniform returns False and that is correct.

Example of table in which the vertical edges of columns do not align

Example of table in which the vertical edges of columns do not align. That table lives up to the criteria for a uniform table  all rows in the table do have the same number of columns. However Table.Uniform returns False.

In case of tables like the ones illustrated in the examples above, it seems correct the Table.Uniform returns False even if they, according to the criterion, should return True. The tables will result in an error if you attempt to access the individual columns.

The VBA macro below works on all columns in all tables in the active document. Error 5992 occurs if a table has mixed cell widths. You will often need to include error handling in your code to handle special problems that may occur.

Sub OperateOnAllColumnsInAllTablesInActiveDocument()
    Dim oTable As Table
    Dim oColumn As Column

    For Each oTable In ActiveDocument.Tables
        For Each oColumn In oTable.Columns
            'Do your work
        Next oColumn
    Next oTable
End Sub

Conclusion – when does Table.Uniform return True? 

For a Word table to be considered uniform via VBA when using Table.Uniform, the table must live up to these criteria:

  • No vertically merged cells must be found in the table
  • No horizontally merged cells must be found in the table
  • All vertical edges in all cells in each column must be precisely aligned, i.e. a column must not have mixed cell widths
  • You need be aware of special behavior of tables pasted from Excel.

How to avoid VBA errors due to tables that are not uniform

The only way to be sure that VBA does not run into errors with tables that are not uniform, you need to operate on the individual cells in tables.

If you need to perform the same operation on all cells in each row or all cells in each column, your VBA code would run faster if you operate or entire rows or entire columns. However, this may result in the run-time errors described above.

If your code has to handle all tables in a document, you could first check whether any tables are found that return Table.Uniform = false. In that case, you can operate on the individual cells.

Below is an example of the main structure of a macro that operates on all cells in all tables in the active document. You will often need to include error handling in your code to handle special problems that may occur. You will often need to include error handling in your code to handle special problems that may occur.

Sub OperateOnAllCellsInAllTablesInActiveDocument()
    Dim oTable As Table
    Dim oCell As Cell

    For Each oTable In ActiveDocument.Tables
        For Each oCell In oTable.Range.Cells
            'Do your work
        Next oCell
    Next oTable
End Sub

Free Trial icon
Word Add-In from DocTools
Did you know that...

DocTools Word Add-Ins
can help you save time in Word

On my website wordaddins.com you will find some of the Word Add-Ins I have developed, ready for use:

Generate complete documents in seconds from re-usable text or graphics - read more...

helps you manage comments in Word fast and easy – review comments, extract comments, etc. - read more...

Makes it easier than ever to work with cross-references in Word documents - read more...

Lets you manage document data efficiently with custom document properties and DocProperty fields - read more...

Lets you extract insertions, deletions and comments in full context and including headings - read more..

Lets you apply or remove any highlight color by the click of a button - read more...

Browse pages, headings, tables, graphic, etc. and find text in Word with a single click - read more...

Browse pages, headings, tables, graphic, etc. and find text in Word with a single click - read more...

Lets you quickly and easily create screen tips in Word with up to 2040 characters - read more...