Identify tables in your document

J

Juan Magaña

Hi everyone,

if I add more than one table in a document, is there a way to put a name to
each of them while I add them, or some other way to reffer to them rather
than the tables(i) value?

Thanks, from Spain
 
J

Juan Magaña

Thanks... I don`t know how to do that but I'll look it up. You mean to mark
it as I add the table?
 
J

Jonathan West

Juan Magaña said:
Thanks... I don`t know how to do that but I'll look it up. You mean to mark
it as I add the table?

That's right. Look up the Add method of the Bookmarks collection.
 
J

Jonathan West

JGM said:
Hi Juan,

Look up the ID property for tables.

Hmm. That's another one I hadn't seen before! :)

However, unless I've missed something, you can only identify the ID of a
table, you can't instantly select a table with a specific ID, in the way
that you could select the Range of a bookmark.
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Juan,

Dim an object as a Table, then set the object to the table that you are
adding and thereafter, refer to the object.

Dim mytable As Table
Set mytable = ActiveDocument.Tables.Add - etc.

then

mytable.Cell(i, j)

with give you a reference to a cell in the table.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

JGM

Hi Jonathan,

Normally you would be right...

But I am not your normal guy!

So here is my "devious" way of using the ID property as if it were a name:
'_______________________________________
Sub SelectTableByName_or_Almost_ByName()

Dim myTable As Table

ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=5, NumColumns:=5
Selection.Tables(1).ID = "TBL1"

Selection.MoveDown Unit:=wdLine, Count:=7

ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=5, NumColumns:=5
Selection.Tables(1).ID = "TBL2"

Selection.MoveDown wdLine, 7

ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=5, NumColumns:=5
Selection.Tables(1).ID = "TBL3"

Selection.MoveDown wdLine, 7

For Each myTable In ActiveDocument.Tables
If myTable.ID = "TBL2" Then
myTable.Select
Exit For
End If
Next

myTable.Rows.Add
myTable.Cell(1, 1).Select
'etc.
End Sub
'_______________________________________

This will select the second table "by name"... and once it is selected, or
identified, you can do whatever you want with it...
So unless there are reasons I ignore for not using the ID property this way,
I think it is pretty neat, no?

Cheers!
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Jean-Guy,

That's right, you're the Jean guy.

This is the way that I would do that:

Dim Table1 As Table, Table2 As Table, Table3 As Table, myrange As Range
Set Table1 = ActiveDocument.Tables.Add(Selection.Range, 5, 5)
ActiveDocument.Range.InsertAfter vbCr & vbCr
Set myrange = ActiveDocument.Range
myrange.Start = myrange.End
Set Table2 = ActiveDocument.Tables.Add(myrange, 5, 5)
ActiveDocument.Range.InsertAfter vbCr & vbCr
Set myrange = ActiveDocument.Range
myrange.Start = myrange.End
Set Table3 = ActiveDocument.Tables.Add(myrange, 5, 5)
Table2.Rows.Add
Table2.Cell(1, 1).Range.Select

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jonathan West

This will select the second table "by name"... and once it is selected, or
identified, you can do whatever you want with it...
So unless there are reasons I ignore for not using the ID property this way,
I think it is pretty neat, no?

That will work OK if you have a small document.

Once you start increasing the number of tables a lot, the speed of your code
might drop alarmingly, especially if the table you need to select is towards
the end of the collection. This is a problem that doesn't occur with
bookmarks, as the bookmarks collection is indexed by name.

I haven't experimented to see how much of a problem this might be with
tables, though I have had experience of very slow performance cyclying
through other large collections of Word objects.
 
J

Jonathan West

"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
Hi Jean-Guy,

That's right, you're the Jean guy.

This is the way that I would do that:

Dim Table1 As Table, Table2 As Table, Table3 As Table, myrange As Range
Set Table1 = ActiveDocument.Tables.Add(Selection.Range, 5, 5)
ActiveDocument.Range.InsertAfter vbCr & vbCr
Set myrange = ActiveDocument.Range
myrange.Start = myrange.End
Set Table2 = ActiveDocument.Tables.Add(myrange, 5, 5)
ActiveDocument.Range.InsertAfter vbCr & vbCr
Set myrange = ActiveDocument.Range
myrange.Start = myrange.End
Set Table3 = ActiveDocument.Tables.Add(myrange, 5, 5)
Table2.Rows.Add
Table2.Cell(1, 1).Range.Select

Hi Doug,

That's also a good solution. It avoids the potential performance problems of
Jean Guy's solution.

The only drawback to it would be if the names needed to be used for running
a second separate macro at a later time. By then, the Table object variables
would have gone out of scope. In that case, something would need to be done
to mark the tables in the document itself.

If however, the identification only needs to last as long as the macro is
running, then this is the best solution.

So Juan, you have a variety of solutions, each of which might be better
depending on exactly what you need to do :)
 
J

Juan

Thanks everyone for your expert answers...

I still am not sure of the final approach I´ll need, but I do know the
solutions is in your answers...

Thanks, from Spain
 

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