PDA

View Full Version : Solved: Index of Selected Table



Lyrad
10-20-2008, 09:47 AM
Hi. I have a lot of tables in my document. My macro inserts a chart below a table that I select. The chart is generated based on the value in the cells of the table I selected.

I have tried but cannot find a means of finding out the index number of the table I have selected. I need this index for my ActiveDocument.Tables(index).

I use this index to work out which cells from which table to reference for my chart. I also need this to make sure the chart gets generated one paragraph below the table I selected.

Any help will be appreciated.

lucas
10-20-2008, 10:57 AM
This might help you with your problem. It is from an earlier post and the info was provided by Gerry(fumie).



The Index of the Tables collection is the order in the document. So if you move the second table - ActiveDocument.Tables(2) - in a document before the first one, then IT automatically becomes ActiveDocument.Table(1).

If you inserted a table before it (while it is still #2), then it automatically becomes ActiveDocument.Tables(3).

Therefore, what I do is bookmark the tables, all of them. Tables usually contain something meaningful, so I use THAT as the name of the bookmark.


If you are adding tables and it would get confusing using the index number because of that you can simply select the table and give it a bookmark name to reference in your code. Hope this helps.

fumei
10-20-2008, 11:58 AM
There is no way to get the index number of the the table the Selection is in, except to actually loop through the Tables collection using the current Selection range number. You can test the current Selection range number and see if it is between each table range Start/End. If it is, then the selection is in that table. However, you are NOT really getting the actual index number. You are incrementing the table testing, and getting THAT number. Like this - the variable j is the incremented counter:


Option Explicit

Sub yadda()
If Selection.Information(wdWithInTable) = True Then
MsgBox ThisTableNumber
Else
MsgBox "Selection is not in a table."
End If
End Sub


Function ThisTableNumber() As String
Dim CurrentSelection As Long
Dim T_Start As Long
Dim T_End As Long
Dim oTable As Table
Dim j As Long
CurrentSelection = Selection.Range.Start
For Each oTable In ActiveDocument.Tables
T_Start = oTable.Range.Start
T_End = oTable.Range.End
j = j + 1
If CurrentSelection > T_Start And _
CurrentSelection < T_End Then
ThisTableNumber = "Selection is in Table " & j
Exit For
End If
Next
End Function

The Function (after the originating Sub tests to see if the Selection actual IS in a table), gets the Selection.Range.Start, then gets the Start and End values of each table. If the Selection.Range.Start is between those numbers, then it has to be in that table.

This is NOT fully reliable, as you can nest tables in tables. A nested table is NOT, repeat NOT, a member of the documents Tables collection. I can not stress this enough. A nested table is NOT a member of the document Tables collection. It is a member of the Table(x).Range.Tables collection.

In other words,

For Each oTable In ActiveDocument.Tables

will NOT, repeat NOT, find a nested table. You would have to add more detailed logic. It can be done however.

If there is a serious need to get the table index number for the Selection, you can put the test to see if the Selection IS in a table right into the Function. Whether you do (or not) depends on your requirements.

IMO, if you are doing a lot of work with tables, it is well worth the effort to bookmark them. Once you do that, you can declare and Set a table object for any given table by name. You can do this regardless of the table index number. The index number means ONE thing, and one thing only. It is the current order of the table in the document. ANY changes (moving the table, adding or deleting other taables) automatically changes the index number. A table index number is a document property...NOT a property of the table.

Lyrad
10-20-2008, 11:19 PM
Thank you lucas and fumei. The solution provided by fumei is what I am after.

I had to learn VBA in one day to do what I had to do and I am not a programmer. The documentation in the object browser is not at all catered for beginners and it was a trial and error effort to get to where I am now with the automatic chart generation, inclusive of labelling, coloring, color pattern ...

I really appreciate the help.

jumpjack
06-09-2010, 08:20 AM
Option Explicit

Sub yadda()
If Selection.Information(wdWithInTable) = True Then
MsgBox ThisTableNumber
Else
MsgBox "Selection is not in a table."
End If
End Sub


Function ThisTableNumber() As String
Dim CurrentSelection As Long
Dim T_Start As Long
Dim T_End As Long
Dim oTable As Table
Dim j As Long
CurrentSelection = Selection.Range.Start
For Each oTable In ActiveDocument.Tables
T_Start = oTable.Range.Start
T_End = oTable.Range.End
j = j + 1
If CurrentSelection > T_Start And _
CurrentSelection < T_End Then
ThisTableNumber = "Selection is in Table " & j
Exit For
End If
Next
End Function


old thread, good solution... little mistake: there are two missing "=" in the above source; this is the right one:



Option Explicit

Sub yadda()
If Selection.Information(wdWithInTable) = True Then
MsgBox ThisTableNumber
Else
MsgBox "Selection is not in a table."
End If
End Sub


Function ThisTableNumber() As String
Dim CurrentSelection As Long
Dim T_Start As Long
Dim T_End As Long
Dim oTable As Table
Dim j As Long
CurrentSelection = Selection.Range.Start
For Each oTable In ActiveDocument.Tables
T_Start = oTable.Range.Start
T_End = oTable.Range.End
j = j + 1
ThisTableNumber ="Couldn't determine table number" ' Added error message
If CurrentSelection >= T_Start And _
CurrentSelection <= T_End Then ' added "="
ThisTableNumber = "Selection is in Table " & j
Exit For
End If
Next
End Function

fumei
06-09-2010, 08:36 AM
Good catch.

davidtheterp
12-14-2011, 09:31 AM
I found a faster way! Thanks fumei and jumpjack for your submission. It also worked quite well.

' Rapid method from word.mvps.org/faqs/MacrosVBA/GetIndexNoOfPara.htm
MsgBox ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count