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 / December 2007

Tip: Looking for answers? Try searching our database.

Identify a Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sridhar - 10 Dec 2007 02:42 GMT
Is there any way to Identify a particular table from the list of available
tables in the word page.

I have a Single Page Word document which has 8 tables inserted from
different files. Now i need to fill a particular table with values.
When i used Thisdocument.Tables (Number) Sometimes i am getting the right
table and some times not the right one.
How can i fill data to the right table.

Thanks
Sri
fumei - 12 Dec 2007 20:17 GMT
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
Tony Strazzeri - 24 Dec 2007 05:39 GMT
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
 
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.