How to get the Rowspan and Colspan of a table cell using VBA

Article contributed by Klaus Linke and Jeff Hall


You can get the number of spanned rows, if you select the cell using myCell.Select, and then use

RowSpan = (Selection.Information(wdEndOfRangeRowNumber) - _
      Selection.Information(wdStartOfRangeRowNumber)) + 1

Unfortunately, this method does not work if you use ranges, so:

Dim MyRange As Range
MyRange = ActiveDocument.Tables(1).Cell(1, 1).Range
RowSpan = (MyRange.Information(wdEndOfRangeRowNumber) - _
    MyRange.Information(wdStartOfRangeRowNumber)) + 1

does not work (this is a bug).

Also, this method does not work on a table cell that has been selected manually with the mouse, and nor does it work if you use Selection.Expand Unit:=wdCell to select the cell. You have to use MyCell.Select, e.g.:

Dim MyCell As Cell
For Each MyCell In ActiveDocument.Tables(1).Range.Cells
    Msgbox "Rowspan = " & _
        (Selection.Information(wdEndOfRangeRowNumber) - _
        Selection.Information(wdStartOfRangeRowNumber)) + 1
Next MyCell


There doesn't seem to be a straightforward way to determine the number of columns that a merged cell spans. But the best workaround seems to be as follows:


Calculate the total width of the table (in points).


Get the width of current cell in points.


Convert the cell width to a percentage of the table width (eg 27%).


use <TD COLSPAN=27 WIDTH=27%>.

This effectively breaks a table into 100 vertical columns, each 1% of the table width. The browser can easily display the cells accurately even when there are staggered or horizontally merged cells. No need for a load of complicated logic.

Note that you should not use COLS=100 in the <TABLE> definition because (surpise surpise) Netscape does not like it!!

Click to view Terms of Use page

Click to view Disclaimer page