The function below will return an array. The array will contain an array of table index numbers that match the title number.
Function GetTableIndexNumbers( _
ByVal strTitle As String) As Variant
'Tested 20090214
On Error GoTo errHandler1
Dim arrTemp() As Variant
Dim i As Long
Dim arrGeneralIndex As Long
ReDim arrTemp(0)
'if the number of tables
'is greater than 0
If Application.ActiveDocument.all. _
tags("table").Length > 0 Then
'Loop through each table.
For i = 0 To Application.ActiveDocument. _
all.tags("table").Length - 1
'Make it case non-sensitive.
If UCase(Application.ActiveDocument.all. _
tags("table").Item(i).Title) = _
UCase(strTitle) Then
'increase the index number by 1
arrGeneralIndex = arrGeneralIndex + 1
'make the array 1 value larger.
ReDim Preserve arrTemp(arrGeneralIndex)
'put the latest table number
'in the index just created
arrTemp(arrGeneralIndex) = i
'Exit Function
End If
Next i
End If
GetTableIndexNumbers = arrTemp
Exit Function
errHandler1:
'Notify user of failure.
MsgBox "GetTableItemNumber has failed", _
vbCritical, "Function Failure"
End Function
A typical method of calling this function is shown below.
Sub CallGetIndexTitleNumbers()
Dim strTitle As String
Dim arrGeneral As Variant
Dim strResult As String
Dim i As Long
Dim strAllHave As String
Dim strBoth As String
Dim bytMsgBoxResult As Byte
strTitle = "abc"
strAllHave = " both have title "
strBoth = " both "
arrGeneral = _
GetTableIndexNumbers(strTitle)
If UBound(arrGeneral) > 2 Then
strAllHave = " all have titles "
strBoth = " all "
End If
Select Case UBound(arrGeneral)
Case 0
MsgBox _
"No Tables found with title " & _
"""" & strTitle & """", _
vbCritical, "Table not found"
Case 1
'No message needed here.
'Process the 1 table found.
Case Is > 1
'format index values for Msgbox
For i = 1 To _
UBound(arrGeneral) - 2
strResult = strResult & _
arrGeneral(i) & ", "
Next i
strResult = strResult & _
arrGeneral(i)
strResult = strResult & _
" and " & _
arrGeneral(i + 1)
bytMsgBoxResult = _
MsgBox("Tables " & _
strResult & _
strAllHave _
& """" & strTitle & """" _
& vbCrLf & _
"Process" & _
strBoth & _
"of them?", _
vbYesNoCancel, _
"More than 1 Table Found")
End Select
'If user did not choose either
'"No" or "Cancel"
If bytMsgBoxResult <> 2 And _
bytMsgBoxResult <> 7 Then
'Loop through each table with that title
For i = 1 To UBound(arrGeneral)
'All of your code to process this
'table goes here.
Debug.Print arrGeneral(i)
Next i
End If
Exit Sub
errHandler1:
MsgBox "CallGetIndexTitleNumbers has failed"
End Sub
The Msgbox formatting above will produce the following message if two tables are found.

If more than two tables are found, the message will change to:
