PDA

View Full Version : accessing nested tables



sam_sam
02-11-2007, 09:55 AM
Hi,
I have 2 tables, table1 and table2.
table 2 is inside table1, how can i access table2 by script?
when I try tables.count it displays only 1.
Anyway to get a handler on table2?

thx.
sam.

lucas
02-11-2007, 11:13 AM
Hi Sam,
I'm not great a Word but what I have learned from reading in this forum is that using bookmarks seems to be the most advantagous method for placing and retrieving data on a Word document.

This thread gives a great function provided by Malcolm from one of Steiner's kb entries for putting your userform texbox value into the location of a bookmark on the page. It's just as easy to retrieve that value.
http://www.vbaexpress.com/forum/showthread.php?t=11427

fumei
02-11-2007, 01:40 PM
Yes, you can access a nested table by script. You need the proper parent object reference.

Scenario: one table in document. It contains one table (Table2). The Document object has a table count of 1.

SET a table object as Table1. THAT object now has table count of 1....that is, Table2.
Dim aTable As Table
Dim sCellText As String
Set aTable = ActiveDocument.Tables(1)
sCellText = aTable.Tables(1).Cell(2, 2).Range.Text
sCellText = Left(sCellText, Len(sCellText) - 2)
MsgBox sCellText
Set aTable = NothingThis would display the contents of Cell(2,2) of Table2 (the one INSIDE Table 1).

The table inside Table 1 is a child object of Table 1, NOT a child object of the Document. You need to use the proper parent when making reference to it.

Demo in attached file. Macro to demo can be fired from "Table Cells" icon on top toolbar.

Bookmarks of course are referenced directly, as part of the Document Bookmark collection. It makes no difference if they are in a table within a table. So, yes, it could very well be better to use Bookmarks.

Bookmarks are good.

fumei
02-11-2007, 01:44 PM
The point though is that you CAN access nested tables. There could be a valid reason for not having a bookmark there. Say, you just want to test the contents.

sam_sam
02-17-2007, 04:08 AM
Hi,
Thx for the reply, I need to access a nested table so that i can add new rows to that table.
I actually did the following:
activedocument.tables(1).tables(2)
this way i get a handle to the inside table and can add rows.
The only trick is that my word document can be updated so for example table 2 can be table 3 if i add a new table before it.
Is there a way to give my table an id? this way i can access it by name directly? same as html (document.getElementById('tableid')).
Is it possible?

thanks.

fumei
02-17-2007, 08:48 AM
Bookmark it. Then you can use the bookmark name. As bookmarks can be nested, you can make separate bookmarks of the whole table, and the nested one as well.

Select the table (either the nested one or the whole table) and bookmark it. Now you can use, for example:Debug.Print ActiveDocument.Bookmarks("NestedTable").Range _
.Tables(1).Cell(1, 1)
' OR
Debug.Print ActiveDocument.Bookmarks("NestedTable").Range _
.Cells(1)Both will return the contents of Cell 1,1 of the nested table.

OR, again, use a table object. You could use, once you have bookmarked them:Dim NestedTable As Table
Set NestedTable = = ActiveDocument.Bookmarks("NestedTable") _
.Range.Tables(1)
Debug.Print NestedTable.Cell(1,1)It specifies the table, and placing any new tables before it will make no difference at all.

To add another row to that table....NestedTable.Rows.Add

fumei
02-17-2007, 08:50 AM
mmmmmm, objects....

sam_sam
02-18-2007, 10:03 AM
thx.
I will try it.

sam_sam
02-20-2007, 02:12 AM
thanks.
It worked.

fumei
02-20-2007, 11:46 AM
Excellent!