?
Solved

Word VBA select nested table

Posted on 2011-10-03
7
Medium Priority
?
1,585 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:MrDavidThorn
  • 4
  • 2
7 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 1200 total points
ID: 36902389
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
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 800 total points
ID: 36902443
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36902460
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36902479
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36902540
Sorry Chris, I didn't refresh. I see that we have collapsed in opposite directions.  :>)
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36902563
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
 

Author Closing Comment

by:MrDavidThorn
ID: 36902679
Both solutions sutied my requirements. Thanks guys!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question