Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining the table number in word

Status
Not open for further replies.

hankpage

Technical User
Apr 12, 2004
14
US
I am using a word document to build a report. I want to add several tables at the end of the report and plan to use a visual basic program to build the tables and insert data. Right now I am building the tables with a macro in word that I will insert into the visual basic program after getting it to work correctly. I am having trouble selecting a table once more than one table is built. When the macro gets to the following code it gives me a "The requested member of the collection does not exist" error. Why does word think there is not a table 2 when I just have it check to see how many tables are in the document?


K = ActiveDocument.Tables.Count
Selection.Tables(K).Select
 
Hi,

How 'bout
Code:
With ActiveDocument
  .Tables(.Tables.Count).Select
End With


Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Thanks Skip. That works but doesn't solve my problem which is what I should have asked to begin with. I need to get to the first cell in the table so I can start inserting data. I was trying to use this command.

Selection.HomeKey Unit:=wdStory

That works fine on the first table but it keeps returning to the first table every time. I'm sure there is a very simple way to get to the first cell but I can't seem to find it.
 
Code:
With ActiveDocument
  .Tables(.Tables.Count).Rows(1).Cells(1).Select
End With

Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
That worked. I had merged some cells but it worked after I broke the command into two parts.

Thanks for the help. (Man that was fast!)
 


[rockband] Tek-Tips ROCKS!

Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Just remember that Skip's code will always select the very last table in the document, regardless of how many there are. It uses (.Tables.Count).

Here are a couple of other tips regarding tables.

All range objects have a Tables collection. For example, to action all the tables on the current page, use the "\page" bookmark range.
Code:
Dim mTable As Table
For Each mTable In ActiveDocument.Bookmarks("\page").Range.Tables()
    mTable.Rows(1).Cells(2).Range.Text = "whatever"
Next

Use wdWithinTable to check to see if the selection is actually in a table. The follow checks, and if it is in a table, selects the second cell of Row 2 in the current table.

Code:
If Selection.Information(wdWithInTable) Then
   With ActiveDocument.Bookmarks("\table")
     .Range.Rows(2).Cells(2).Select
   End With
Else
   MsgBox "Not in a table"
End If




Gerry
 
Thanks for the tips Gerry. They will be very helpful.

I think I'm beginning to get a handle on how this works.

It seems like Table(1) always works on the selected table instead of the first table. Is that true and if so, what's the deal?
 
There's a difference between Selection.Tables(1) and ActiveDocument.Tables(1)

Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
True. ActiveDocument.Tables(1) points to the first table in the document. Selection.Tables(1) points to the first table in the Range object of the selection. It returns an run-time error if there is no table in the selected range. As does ActiveDocument.Tables(1) if there are no tables.

If possible it better to avoid making Selections. It is better to use Range objects where ever possible. Using Bookmarks is a very good way to get chunks of a document and doing stuff with them. All bookmarks have a range (they are in fact ranges) Start and End. You can use these.

Say you have a document with a bunch of tables grouped together. Lots of text, then a bunch of tables, then a lot more text.

Set a bookmark ("TablesStart") at the start of the table area. Set another at the end ("TablesEnd"). You can put as much other stuff between these, or outside as you like.

To process all the tables, regardless of what page they are on, between the bookmarks and ONLY that range, you set a range:

Code:
Dim aDoc as Document
Dim r As Range
Dim mTable As Table

Set aDoc = ActiveDocument
Set r = aDoc.Range(Start:= _
   aDoc.Bookmarks("TablesStart").End, _
 End:= aDoc.Bookmarks("TablesEnd").Start)

For Each mTable In r.Tables()
    mTable.Rows(1).Cells(2).Range.Text = "whatever"
Next
Set r = Nothing
Set aDoc = Nothing

By explicitly creating a Range, you can process within that range - this could leave other tables alone, or whatever. Nothing is ever Selected, so it is not visible to the user, and could in fact be run in the background on a document the user never even sees.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top