Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Number of rows in a specific table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Newbie - 28 Oct 2006 17:13 GMT
Hello,
How can we know (with VBA) the number of rows in a specific table in a Word
document ?
Thanks in advance!
Jay Freedman - 28 Oct 2006 18:38 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Hello,
>How can we know (with VBA) the number of rows in a specific table in a Word
>document ?
>Thanks in advance!
Newbie - 28 Oct 2006 22:27 GMT
Thanks a lot Jay !

> 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
[quoted text clipped - 12 lines]
> >document ?
> >Thanks in advance!
Newbie - 28 Oct 2006 22:32 GMT
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

> 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
[quoted text clipped - 12 lines]
> >document ?
> >Thanks in advance!
Jay Freedman - 28 Oct 2006 22:51 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Jay,
><<There are many ways to specify which table you're interested in. >>
[quoted text clipped - 23 lines]
>> >document ?
>> >Thanks in advance!
Newbie - 29 Oct 2006 12:15 GMT
Thanks a lot Jay for this detailed answer!

Newbie

> Besides the two methods you mentioned (of which the bookmark is more
> reliable), here are three more:
[quoted text clipped - 72 lines]
> >> >document ?
> >> >Thanks in advance!

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.