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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

GrahamSkanRetiredCommented:
Sorry Chris, I didn't refresh. I see that we have collapsed in opposite directions.  :>)
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
MrDavidThornAuthor Commented:
Both solutions sutied my requirements. Thanks guys!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.