Word VBA select nested table

Hi Experts

Quite a simple one, I have a nested table in word, when the cursor is in nested level 2, I require a macro to select the first row of nested level 1.

Overalll what I want to achieve is the automation of a selected chart copied over to powerpoint, I have the code to select the chart index and to copy the chart over to powerpoint, problem is if the cursor is in anything above nested level 1, it will only take part of the table.
MrDavidThornAsked:
Who is Participating?
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Add the following function in a normal code module then for example to select:

First table in the selection
getparenttable(selection.Tables(1)).select

specific table ... 1 in the activedocument
getparenttable(activedocument.Tables(1)).select

Function getParentTable(tbl As Word.Table) As Word.Table
Dim outerTable As Table
Dim rng As Range
    
    Set outerTable = tbl
    Do While outerTable.NestingLevel > 1
        Set rng = outerTable.Range
        rng.Collapse Direction:=wdCollapseEnd
        rng.MoveEnd Unit:=wdCharacter, Count:=1
        Set outerTable = rng.Tables(1)
    Loop
    Set getParentTable = outerTable

End Function

Open in new window

0
 
GrahamSkanRetiredCommented:
Does this suit?
Sub SelectOuterTable()
    Dim rng As Range
    
    If Selection.Tables(1).NestingLevel = 2 Then
        Set rng = Selection.Tables(1).Range
        rng.Collapse wdCollapseStart
        rng.Move wdCharacter, -1
        rng.Tables(1).Range.Cells(1).Range.Select
    End If
End Sub

Open in new window

0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Looking at why graham posted I noted I overlooked the row one aspect ... But that is as easy as

First table in the selection
getparenttable(selection.Tables(1)).rows(1).select

specific table ... 1 in the activedocument
getparenttable(activedocument.Tables(1)).rows(1).select

Note also my code will select the outermost table irrespective of how many tables are nested.

Chris
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Chris BottomleySoftware Quality Lead EngineerCommented:
Also please note that whilst the method I posted seems more complex this is because I explicitly chose to be more flexible in that I return the table so it can be processed in more ways rather than embedding it to make it use specific .... Which would be for example

getparenttable

Chris
Function getparenttable()
Dim outerTable As Table
Dim rng As Range
    
    Set outerTable = selection.tables(1)
    Do While outerTable.NestingLevel > 1
        Set rng = outerTable.Range
        rng.Collapse Direction:=wdCollapseEnd
        rng.MoveEnd Unit:=wdCharacter, Count:=1
        Set outerTable = rng.Tables(1)
    Loop
    outerTable.rows(1).select

End Function

Open in new window

0
 
GrahamSkanRetiredCommented:
Sorry Chris, I didn't refresh. I see that we have collapsed in opposite directions.  :>)
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Graham

Usefully your post prompted me that I wasn't showing how to select the row. As for collapsing I tend to collapse in every direction .... Some might say I explode :)

Chris
0
 
MrDavidThornAuthor Commented:
Both solutions sutied my requirements. Thanks guys!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.