Number of rows in a specific table

N

Newbie

Hello,
How can we know (with VBA) the number of rows in a specific table in a Word
document ?
Thanks in advance!
 
J

Jay Freedman

There are many ways to specify which table you're interested in. Once
you have a table, though, getting its number of rows is simply

myTable.Rows.Count

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
N

Newbie

Thanks a lot Jay !

Jay Freedman said:
There are many ways to specify which table you're interested in. Once
you have a table, though, getting its number of rows is simply

myTable.Rows.Count

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
N

Newbie

Jay,
<<There are many ways to specify which table you're interested in. >>
I know only 2 ways to do that:
- The rank of the table in the Word document (but it can change if we
insert another table)
- To attach a bookmark to the table

Do you know a better way to refer a specific table ?
Thanks
 
J

Jay Freedman

Besides the two methods you mentioned (of which the bookmark is more
reliable), here are three more:

(1) If the cursor is in the table (that is, you're assuming the user
will choose the table before starting the macro), then it's
Selection.Tables(1). That refers to the "first table of which any part
is in the Selection" regardless of the table's index in the total
document.

(2) If you're applying the same actions to all the tables in the
document, you can use the For Each construct:

Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
' do something with oTbl
Next

(3) Use the .Find method of a Range object to search for some text
that may be in the table, and verify that it's in a table before doing
some work:

Dim oRg As Range
Dim oTbl As Table

Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = "look for this"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
Do While .Execute
If oRg.Information(wdWithInTable) Then
Set oTbl = oRg.Tables(1)
' do something with oTbl
End If
Loop
End With

There are probably others, but that covers the most common ones. :)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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