The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
The purpose of this Microsoft Word Tips & Microsoft Word Help page is to introduce and publish my version of a Table & Cell Data template add-in that I find more useful and reliable than the TableCellHelper macro provided by Microsoft in earlier Word versions and distributed with the Macros8.Dot template.
Actually, I have never been able to get Microsoft's version to work reliably at all. If you are interested in experimenting with Microsoft's TableCellHelper macro you can download it here: Macros8.
I call my add-in TableCellData. Unlike WordPerfect, Word has a shortcoming when it comes to determining the location of the IP or selection in a table. This can be frustrating when you are working in a table with many rows or columns. TableCellData can provide this and other information about your tables as either a message box or status bar display or both.
The illustration below shows the standard status bar report for a single uniform table (i.e., a table with no split or merged cells)
Table cells can be merged or split. For this reason, TableCellData reports the maximum "Max" column and row count. For example, in the following table, cell C5 has been split into three cells. Therefore there are now 12 Max columns. While the hard column headings indicate the cursor is in column "J," notice the cursor is location is accurately reported as L5. This would be important if you needed to reference this cell's value in a calculation field.
TableCellData can also report the range of selected cells. This feature is only 100% accurate in uniform tables (i.e., tables without split or merged cells).
Word can determine if a table has split or merged cells, but it can't determine if there are split or merged cells in the current selection. Therefore, all selection span reports on tables that contain split or merged cells are susceptible to error.
Note: Microsoft's TableCellHelper has the same problems but it doesn't tell you about it.
Reports for selections containing split or merged cells may or may not be accurate depending on the relationship of the selected cells to other split or merged cells. This is shown in the following illustrations:
I've placed double formula fields in these illustrations. One to show you the field code and one to show the result.
There is no readily available VBA solution to this range reporting problem. It is pretty much hit and miss. Therefore I have provided an option in the TableCellHelper code to completely suppress span reports if the table contains split or merged cells. An example of the suppressed report is shown below.
TableCellData will identify nested tables down two levels. The following illustration will help you understand how information for nested tables is reported. The cursor is located in cell A2 of the second table in cell B3 of the first table in cell B1 of the second table of the document. While the table IDs are reported, only the cell ID of the lowest level nested cell is reported.
Note: To determine the cell ID of a parent cell (i.e., a cell containing a nested child table) you must put the cursor directly in the cell proper (i.e, in the cell, but outside of any nested table in the cell).
The VBA code required to report table cell data is provided in the code pane below:
Option Explicit 'Main procedure. Sub TableCellData() Dim i As Long, oColMax As Long, oRowMax As Long, oCellCnt As Long Dim strColStart As String, strRowStart As String Dim strColEnd As String, strRowEnd As String Dim strMsg1 As String, strMsg2 As String, strMsg3 As String, strMsg4 As String, strMsg5 If Application.Documents.Count Then With Selection If .Information(wdWithInTable) Then i = ActiveDocument.Range(0, .Tables(1).Range.End).Tables.Count strMsg1 = TableConfigID(i) oColMax = .Tables(1).Columns.Count oRowMax = .Tables(1).Rows.Count oCellCnt = .Tables(1).Range.Cells.Count strMsg2 = "/Max. columns: " & oColMax & "/" _ & "Max. rows: " & oRowMax strMsg3 = "Total cells: " & oCellCnt & ". " strColStart = alphaChar(.Information(wdStartOfRangeColumnNumber)) strRowStart = .Information(wdStartOfRangeRowNumber) strColEnd = alphaChar(.Information(wdEndOfRangeColumnNumber)) strRowEnd = .Information(wdEndOfRangeRowNumber) Select Case .Cells.Count Case Is < 2 strMsg4 = "At cell: " & strColStart & strRowStart & ". " If Not .Tables(1).Uniform Then strMsg5 = "This table contains split or merged cells." End If Case Else If .Tables(1).Uniform Then strMsg4 = "Selection spans: " _ & strColStart & strRowStart & ":" & strColEnd & strRowEnd & "." Else 'Stet as appropriate to suppress or report spans in tables with split or merged cells strMsg4 = "Table contains split/merged cells." _ & " The span can not be positively determined." 'strMsg4 = "Selection spans: " & strColStart & strRowStart & ":" _ & strColEnd & strRowEnd & ". Span susceptible to" _ & " error due to split/merged cells." strMsg5 = "" End If End Select 'Can use message box, status bar, or both MsgBox strMsg1 & strMsg2 & vbCr & vbCr & strMsg3 _ & vbCr & vbCr & strMsg4 & vbCr & vbCr & strMsg5, _ vbInformation + vbOKOnly, "Table Data" Application.StatusBar = strMsg1 & strMsg2 & "/" & strMsg3 & strMsg4 & strMsg5 End If End With End If lbl_Exit: Exit Sub End Sub Function TableConfigID(ByVal i As Long) As String Dim TopTbl As Table, Nest1Tbl As Table, Nest2Tbl As Table Dim x As Long, y As Long Dim ttCell As Word.Cell, ntCell As Word.Cell Dim tmpMsg1 As String, tmpMsg2 As String, tmpMsg3 As String Set TopTbl = ActiveDocument.Tables(i) tmpMsg1 = "Table " & i x = 0 For Each ttCell In TopTbl.Range.Cells If ttCell.Tables.Count > 0 Then For Each Nest1Tbl In ttCell.Tables x = x + 1 If Selection.InRange(Nest1Tbl.Range) Then tmpMsg1 = "Table " & i & "{Table " & x & "}" End If y = 0 For Each ntCell In Nest1Tbl.Range.Cells If ntCell.Tables.Count > 0 Then For Each Nest2Tbl In ntCell.Tables y = y + 1 If Selection.InRange(Nest2Tbl.Range) Then tmpMsg1 = "Table " & i & "{Table " & x & "{Table " & y & "}}" End If Next Nest2Tbl End If Next ntCell Next Nest1Tbl End If Next ttCell TableConfigID = tmpMsg1 lbl_Exit: Exit Function End Function Function alphaChar(pAribicNum As Integer) As String Select Case pAribicNum Case Is < 27 alphaChar = String(1, (pAribicNum + 64)) Case Is < 53 alphaChar = "A" & String(1, (pAribicNum - (26) + 64)) Case Is >= 53 alphaChar = "B" & String(1, (pAribicNum - (52) + 64)) End Select lbl_Exit: Exit Function End Function
To use TableCellData with downloading the add-in, simply copy the procedures to a module in any document VBA project.
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
In the add-in, I've included a dynamic control to the "Tables" menu. The control is only enabled when the selection is located in a table.
You can download the template add-in here: TableCellData
For more on template add-ins and how to load them, see: Organizing Your Macros/Template Add-ins at: Installing Macros
Note: This tips page, illustrations and examples were developed using Word 2003. It has not been tested in Word 2007/2010.
That's it! I hope you have found this tips page and the add-in useful and informative.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!