A function which returns the "index" of a table

T

TimvG

A few years back somebody asked how one would find out the "index" of
a nested table. By this they meant that if you a bunch of tables
nested inside another table (all at the same nesting level), how do
you find out whether your currently selected table is the first, the
second, the third, or... It seems that they didn't get the answer
they needed and the matter was dropped.

As it happens I met this same problem today. Here's a solution:

Function TableNumber()

Dim TempRange As Range
Dim TableIndex As Integer
Dim aTable As Table

If Selection.Information(wdWithInTable) And
Selection.Tables(1).NestingLevel > 1 Then
Let Selection.Tables(1).ID = "Fred"
Set TempRange = Selection.Range
Do While Selection.Tables(1).NestingLevel =
TempRange.Tables(1).NestingLevel
Selection.MoveDown
Loop
Let TableIndex = 0
For Each aTable In Selection.Cells(1).Tables
TableIndex = TableIndex + 1
If aTable.ID = "Fred" Then
TableNumber = TableIndex
Exit For
End If
Next
TempRange.Select
Let Selection.Tables(1).ID = ""
MsgBox TableNumber
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top