Locating specific tables

K

Kevin B

How does one identify a table's index number other than counting them?

I'm populating Word tables from a PowerBuilder application and as long as I
know the table's index number I can reference it from PowerBuilder without a
problem, but I have to count the table's position in the document manually in
order to determine what table I'm trying to populate.

I looped through all the tables in the target document using the code below
hoping I would get some table identifier, and all I got was 30 lines of
"Table Number:"

Sub TableIDs()

Dim doc As Document
Dim tbl As Table
Dim strTables As String
Dim i As Integer
Set doc = ThisDocument

i = 1

For Each tbl In doc.Tables
strTables = strTables & "Table Number: " & tbl.ID & vbCrLf
Next tbl

Debug.Print strTables
Set Doc = Nothing
End Sub

Thanks ahead of time.
 
M

macropod

Hi Kevin,

Unless you assign an ID to a table or you bookmark the table, the only way of identifying it is by looping through the tables
collection to see which one the selection is in. For example:
Sub GetTableIndex()
Dim i As Integer
If Selection.Information(wdWithInTable) = True Then
For i = 1 To ActiveDocument.Tables.Count
If (Selection.Range.Start >= ActiveDocument.Tables(i).Range.Start) And _
(Selection.Range.End <= ActiveDocument.Tables(i).Range.End) Then
MsgBox "The selection is in Table " & i
Exit For
End If
Next i
End If
End Sub

To give each table an ID, you could use something like:
Sub SetTableIDs()
Dim i As Integer
With ActiveDocument
For i = 1 To .Tables.Count
.Tables(i).ID = "Table" & i
Next i
End With
End Sub

Then, to retrieve the current table's ID you could use:
Sub GetTableID()
If Selection.Information(wdWithInTable) = True Then _
MsgBox "The selection is in " & Selection.Tables(1).ID
End Sub
 
K

Kevin B

That's just what I needed.

Thanks
--
Kevin Backmann


macropod said:
Hi Kevin,

Unless you assign an ID to a table or you bookmark the table, the only way of identifying it is by looping through the tables
collection to see which one the selection is in. For example:
Sub GetTableIndex()
Dim i As Integer
If Selection.Information(wdWithInTable) = True Then
For i = 1 To ActiveDocument.Tables.Count
If (Selection.Range.Start >= ActiveDocument.Tables(i).Range.Start) And _
(Selection.Range.End <= ActiveDocument.Tables(i).Range.End) Then
MsgBox "The selection is in Table " & i
Exit For
End If
Next i
End If
End Sub

To give each table an ID, you could use something like:
Sub SetTableIDs()
Dim i As Integer
With ActiveDocument
For i = 1 To .Tables.Count
.Tables(i).ID = "Table" & i
Next i
End With
End Sub

Then, to retrieve the current table's ID you could use:
Sub GetTableID()
If Selection.Information(wdWithInTable) = True Then _
MsgBox "The selection is in " & Selection.Tables(1).ID
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Kevin B said:
How does one identify a table's index number other than counting them?

I'm populating Word tables from a PowerBuilder application and as long as I
know the table's index number I can reference it from PowerBuilder without a
problem, but I have to count the table's position in the document manually in
order to determine what table I'm trying to populate.

I looped through all the tables in the target document using the code below
hoping I would get some table identifier, and all I got was 30 lines of
"Table Number:"

Sub TableIDs()

Dim doc As Document
Dim tbl As Table
Dim strTables As String
Dim i As Integer
Set doc = ThisDocument

i = 1

For Each tbl In doc.Tables
strTables = strTables & "Table Number: " & tbl.ID & vbCrLf
Next tbl

Debug.Print strTables
Set Doc = Nothing
End Sub

Thanks ahead of time.
 

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