Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to find the index number of the table if it is a nested table.

6 views
Skip to first unread message

Cindy M -WordMVP-

unread,
May 20, 2004, 11:17:15 AM5/20/04
to
Hi =?Utf-8?B?T2xnYVA=?=,

> I have several tables nested within a table. How do I find an index of the nested table.
>
What kind of index?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the
newsgroup and not by e-mail :-)

Doug Robbins - Word MVP

unread,
May 21, 2004, 2:38:36 AM5/21/04
to
Can you get by with Selection.Tables(1) That will give a reference to the
table in which the selection is located.

--
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
"OlgaP" <anon...@discussions.microsoft.com> wrote in message
news:5B520DD8-569C-472D...@microsoft.com...
> I have several tables nested in a table. When user makes selection in one
of the tables, I need to know which table he/she is in.

Doug Robbins - Word MVP

unread,
May 21, 2004, 6:17:45 PM5/21/04
to
Based on my attempts, I don't think that you can.

It doesn't matter how many nested tables there are, if you use
ActiveDocument.Tables.Count, it will only return the number of "un-nested"
tables in the document.

--
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
"OlgaP" <anon...@discussions.microsoft.com> wrote in message

news:9CDD0ADF-158C-4962...@microsoft.com...
> Hi Doug! I understand that I can get a reference to the selected table. I
need to know what is the number of this table. If there are 5 nested tables
inside the main table, I need to know what is the number of the table that
the user is in, is it one, two, three, four or five. Thanks for your help.

Cindy M -WordMVP-

unread,
May 22, 2004, 5:41:26 AM5/22/04
to
Hi =?Utf-8?B?T2xnYVA=?=,

> I understand that I can get a reference to the selected table. I need to know what is the number of this table. If there are 5 nested tables inside the main table, I need to know what is the number of the table that the user is in, is it one, two, three, four or five.
>

Try it using this:

Selection.Tables(1).NestingLevel

Doug Robbins - Word MVP

unread,
May 22, 2004, 8:33:09 AM5/22/04
to
But if you have two tables nested in different cells of the base table, it
will not tell you in which one the selection is located. Both of the nested
tables have a NestingLevel of 2

--
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

"Cindy M -WordMVP-" <C.Mei...@hispeed.ch> wrote in message
news:VA.000098a0.005671b3@speedy...

Cindy M -WordMVP-

unread,
May 23, 2004, 4:54:06 AM5/23/04
to
Hi Doug,

> But if you have two tables nested in different cells of the base table, it
> will not tell you in which one the selection is located. Both of the nested
> tables have a NestingLevel of 2
>

That's not how I understood the original question; I thought the user was
asking for the nesting level. As to getting the "index" of the outermost
table...

You really can't get that information directly, even in non-nested tables, can
you? But this works:

Sub MoveToTopLevelTable()
Dim l As Long
Dim rng As Word.Range
Dim nrTables As Long
Dim rngTables As Word.Range

If Selection.Information(wdWithInTable) Then
l = Selection.Tables(1).NestingLevel
Set rng = Selection.Tables(1).Range
Do While l > 1
rng.Collapse wdCollapseEnd
l = rng.Tables(1).NestingLevel
Set rng = rng.Tables(1).Range
Loop
Set rngTables = ActiveDocument.Range
rngTables.End = rng.End
Debug.Print "The selection is nesting in level " & _
CStr(Selection.Tables(1).NestingLevel) & " in table " _
& rngTables.Tables.Count & " of the document."
End If
End Sub

Doug Robbins - Word MVP

unread,
May 23, 2004, 5:59:58 AM5/23/04
to
Hi Cindy,

I guess I am not sure what the OP meant by Index.

While I can think of situations where you might want a table with
NestingLevel of 2, I can't really think of a reasonable use for one nested
deeper than that. I can however think of a situation where you may want
multiple tables with a Nesting Level of 2 in a single table e.g. using a
table to maintain a side by side translation of a document containing
tables.

Here's another method of doing what your macro does:

Dim i As Long, j As Long
If Selection.Information(wdWithInTable) Then
j = Selection.Tables(1).NestingLevel
For i = 1 To ActiveDocument.Tables.Count
If Selection.InRange(ActiveDocument.Tables(i).Range) Then
MsgBox "The selection is in a table with Nesting Level " & j & "
in Table " & i & " in the document."
Exit For
End If
Next i
End If

--
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
"Cindy M -WordMVP-" <C.Mei...@hispeed.ch> wrote in message

news:VA.000098b9.007e0838@speedy...

OlgaP

unread,
May 24, 2004, 12:46:04 PM5/24/04
to
Hi Doug and Cindy!
Thanks a lot for your help. Doug is right, I don't need the nesting level, I need to know which table among tables that have nesting level 2 is selected. I am afraid, that Doug is right and I can't find it out. I will have to have the tables inside the talbe rows and that will allow me to locate which table is selected. Let me know if you think here is another way to do it. Thanks again.
Regards, Olga.

Cindy M -WordMVP-

unread,
May 25, 2004, 9:51:30 AM5/25/04
to
Hi =?Utf-8?B?T2xnYVA=?=,

> I don't need the nesting level, I need to know which table among tables that have nesting level 2 is selected. I am afraid, that Doug is right and I can't find it out.
>

I think both of us have shown you how that works. I know I did in my response to Doug, anyway...

0 new messages