VBA: TableIndex?

E

Ed

In VBA code, I can Find a text string, and then check to see if it's in a
table. If it is, I can get the RowIndex and ColumnIndex. But I'm having
trouble getting the "TableIndex", if there is such a thing. I need to be
able to Find the text, then go up to the Caption text to see if it's the
correct table; if not, Find Next - but the "next" one might be interpreted
as the one I just left. If I can get the "TableIndex" and store it, I can
say "If in TableXX (where I just was), Find Next." Then, when I do find the
right table< I can use the Table number in "Select Table(XX).Cell(A, B)
etc."

Any suggestions are appreciated.

Ed
 
J

JGM

Hi Ed,

Have you tried something along the lines of:

'_______________________________________
Dim MyDoc As Document
Dim MyTable As Table
Dim MyText As Range
Dim i As Long, j As Long, k As Long

Set MyDoc = ActiveDocument

For i = 1 To MyDoc.Tables.Count
Set MyTable = MyDoc.Tables(i)
For j = 1 To MyTable.Rows.Count
For k = 1 To MyTable.Columns.Count
Set MyText = MyTable.Cell(j, k).Range
MyText.SetRange MyText.Start, MyText.End - 1
If MyText = "Target text" Then
'Check caption or anything else
End If
Next k 'Get next cell (Column)
Next j 'Get next row
Next i 'Get next table
'_______________________________________

to iterate systematically through every table n the document?

Or, to get the index number of the table at the current selection point, try
something like:
'_______________________________________
Dim myRange As Range
Dim TableCount As Long
Dim TableIndex As Long

TableCount = ActiveDocument.Tables.Count

Set myRange = Selection.Range
myRange.SetRange ActiveDocument.Content.Start, myRange.End

TableIndex = myRange.Tables.Count

If TableIndex = TableCount Then
'you are in the last table!
End If
'_______________________________________

HTH
Cheers!
 
E

Ed

Thank you for the reply. I do have code using the counters, but I thought
it was just my "Neanderthal" method to hobble around on my ignorance of
better methods. I thought perhaps there was some way to find out "the
selection is in Table X of Y tables in this document", and avoid having to
increment counters.

By the way, why do you use Long instead of Integer? Is there an advantage?
Or just personal preference?

Ed
 
E

Ed

Thank you, Jean-Guy, for replying. What I'm doing is taking a text string
and finding it in the document. If it's in a table, I need to get up to the
caption and check that; if it's the right table, then select a certain cell
in that table. The only way I know to do that is
Tables(table_number).Cell(x,y).Select - which means I need the table index
number. Or is there a better way?

Ed
 
E

Ed

Sorry for the double reply. I had a network error and didn't realize this
one got sent through. In between, though, I checked my code and clarified
the issue, which is reflected in the other reply.

Ed
 
J

JGM

Hi Ed,

You could set a range from the point in the table (where the text was found)
to the caption itself. Then, by definition, you would have only one table in
that range and its index number would be 1 by default - as there is only one
table in that range, i.e. MyRange.Tables(1) would always refer to the table
where the text was found.
Or,
You could just create a range object encompassing the whole table in case
you need to refer to it after checking the caption,
something like
Set MyRange = Selection.Tables(1)
'then check the caption
and then set it to nothing after the caption bit has been taken care of.

I use long variables because I was told that nowadays, with 32 bit
computers, it is actually more work for the compiler to strip the last 16
bits to produce an integer than to keep it at 32 bit, (a long variable).
Furthermore, it is my understanding that once the 16 bits have been
stripped, they are not going to be used anyway.... so might as well use long
variables..

Please, if someone can explain it better, I would like to read about it so I
can grasp it better.

HTH
Cheers!
 
E

Ed

Great idea! When I get up to the caption I set it as a range; why not
extend the range up to include the caption and do everything in one range?
I like it! Thank you!

Ed
 
P

Peter Hewett

Hi Ed

There is a tables collection and it's easily indexed or iterated over.

Here's some code that you can adapt to your needs. Currently it does the
following:

1. It loops through ALL tables in the current document
2. It searches each table for the text "FindMe"
3. When it find the text "FindMe" it checks that tables first cell for the
text "Stop"
4. If it finds the text "Stop" it stops searching <-- this is where you
need to do your thing!

Public Sub SearchInTables()
Dim tblTemp As Word.Table

' Do this for ALL tables in the document
For Each tblTemp In ActiveDocument.Tables

' Setup for the search
With tblTemp.Range.Find
.ClearFormatting
.Text = "FindMe"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Find the required text
If .Execute Then

' Does the table contain the text "Stop"
' in it's first cell?
With tblTemp.Cell(1, 1).Range
If Left$(.Text, Len(.Text) - 2) = "Stop" Then

' Do what you want here and then exit the loop
Exit For
End If
End With
End If
End With
Next
End Sub


HTH + Cheers - Peter
 

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