The Tables(Number) reflects one thing, and one thing only. The order in the
document.
Tables(3) is the third table. Tables(6) is the sixth table. If, EVER, the
tables are moved, or a table is inserted before it, then its number in Tables
changes.
In other words, Tables(x) does NOT, repeat NOT, point to a explicit table.
It points to a specific table - the xth table from the start of the document.
If this is a persistent issue, then IMO, the best solution (and one I use as
a matter of course) is to bookmark the tables.
Once bookmarked, you can action a table BY NAME.
For example, while I bookmark all tables in my documents, to keep it simple
let's say you have your document, and it has 8 tables. Say also, you need to
action only one of them.
1. select the table.
2. bookmark it and name it, oh....Yadda
Dim aTable As Table
Set aTable = ActiveDocument.Bookmarks("Yadda").Range.Tables(1)
Ta-da! The Table object aTable now IS that table. The table can be moved
anywhere in the document. It can have rows added, or deleted. It does not
matter. The bookmark stays attached to the table, regardless of where it
goes. And from the bookmark you can make a table object, as in the code
snippet above. Now you can use all the properties and methods of a table
object.
Dim aTable As Table
Set aTable = ActiveDocument.Bookmarks("Yadda").Range.Tables(1)
aTable.Cell(2, 3).Range.Text = "Blah"
would put the text "Blah" into Cell(2, 3) of the set table, regardless of
where that table is.
You can expand this method finer if you want. If it is a specific cell, in a
specific table, then bookmark that cell. Then you can dump content into it
directly via the bookmark.
Keep in mind that you can nest bookmarks. So you can bookmark parts (a cell,
a row...whatever) of a table that is, itself, also bookmarked.
>Is there any way to Identify a particular table from the list of available
>tables in the word page.
[quoted text clipped - 7 lines]
>Thanks
>Sri
You could try setting the Table.ID property on each of the tables and
then check if against the table you have. Unfortunately I don't thnk
there is anyway to specifically select a table by its ID. You would
need to iterate each table in the document and until you found the one
with the correct ID. You need to be aware however that the ID is not
unique.
Have a play with the code below to see what I mean. Run it first on a
blank document then again (leaving the tables that were created the
first time).
Hope this helps.
Cheers
TonyS.
Dim tbl As Table
Dim MyTable As Table
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeParagraph
Set tbl = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=2, NumColumns:=5)
tbl.ID = "MyFirstTable"
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeParagraph
Set tbl = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=2, NumColumns:=5)
tbl.ID = "MySecondTable"
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeParagraph
Set tbl = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=2, NumColumns:=5)
tbl.ID = "MyThirdTable"
Selection.EndKey Unit:=wdStory
For Each tbl In ActiveDocument.Tables
If tbl.ID = "MySecondTable" Then
Set MyTable = tbl
Exit For
End If
Next
MyTable.Select
Shauna Kelly - 26 Dec 2007 00:43 GMT
Sri, Tony
Don't forget that Word loses the .ID properties of tables when it closes
the document. So identifying a table by its .ID works if you create the
table on the fly (as Tony's code does), but the minute you close and
re-open the document, the tables lose their .ID.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
> You could try setting the Table.ID property on each of the tables and
> then check if against the table you have. Unfortunately I don't thnk
[quoted text clipped - 46 lines]
>
> MyTable.Select
Tony Strazzeri - 27 Dec 2007 00:26 GMT
Ah! Thanks Shauna, I didn't know that. I haven't actually used it
myself, I was just trying to think of possible solutions.
Cheers
T.
On Dec 26, 11:43 am, "Shauna Kelly"
<ShaunaKe...@SendNoSpamToShaunaKelly.com> wrote:
> Sri, Tony
>
[quoted text clipped - 57 lines]
>
> > MyTable.Select
fumei - 27 Dec 2007 20:35 GMT
Which is why I suggested using bookmarks. They persist. They are
automatically expandable/resizable. I find them a very handy and easy way to
identify a specific table.
>Ah! Thanks Shauna, I didn't know that. I haven't actually used it
>myself, I was just trying to think of possible solutions.
[quoted text clipped - 9 lines]
>>
>> > MyTable.Select