Selecting trees in Outline View -- do VBA equivalents exist?

V

VespersOf1610

Hi folks,

I'm trying to figure out the VBA and Word object model equivalents of
the following user actions in Word 2003. Word, very usefully, allows a
user to select an entire outline tree with just one click, or
alternatively with three. I'd like to perform this behavior
programmatically, but can't find the methods or properties that
accomplish the tasks.

The Word macro recorder is no help. It records nothing when I perform
these actions.

To reproduce the situation, create any simple outline of several nested
headings (using Word's built-in heading styles) and switch to Outline
View. Expand the outline so that all the headings are visible.

1. Triple click on any parent heading. Word will select that heading
and all its descendants, no matter how deeply nested.

2. Click the "plus" symbol to the left of any parent heading. Word will
(as with the first example) select that heading and all its
descendants.

I have tried all manner of combinations using Selection.Expand and
Selection.Extend to duplicate this behavior programmatically, but with
no luck. Any ideas?

Thanks,
Tom Davey
 
H

Helmut Weber

Hi Tom,
endlessly complicated...
and certainly not a question for the beginners' group.

But you never know (talking to myself), whether there
isn't somebody who does it all like nothing if you help
him over the first hurdle.

So here we go:
Sub test5555()

Dim lPrg As Long ' Paragraph number
Dim rTmp As Range ' temporary range
Dim lOut As Long ' outline level
Selection.Collapse ' just in case
Set rTmp = ActiveDocument.Range(0, Selection.Range.Start + 1)
lPrg = rTmp.Paragraphs.Count
lOut = Selection.Paragraphs(1).OutlineLevel
rTmp.Start = Selection.Paragraphs(1).Range.Start
lPrg = lPrg + 1
On Error GoTo fertig ' german for "done"
While ActiveDocument.Paragraphs(lPrg).OutlineLevel > lOut Or _
ActiveDocument.Paragraphs(lPrg).OutlineLevel = 10
rTmp.End = ActiveDocument.Paragraphs(lPrg).Range.End
lPrg = lPrg + 1
Wend
fertig:
If rTmp.Paragraphs.Count = 1 Then rTmp.Collapse
rTmp.Select

End Sub

The weak point is the error handling.
One could avoid that, for sure.
Means, searching for another method, to precvent lPrg from
reaching a value, that is greater than the number of paragraphs
in the document's main story.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
V

VespersOf1610

Helmut, thank you very much. I'm looking forward to trying out this
code.

I guess I can assume that, if the Word macro recorder won't record it,
then no method or property exists?

if so, that's not really problem, because with this help from you I can
do it msyefl!

Regards,
Tom Davey
 
H

Helmut Weber

Hi Tom,

no, not at all. The macro-recorder simply doesn't record
everything. Some things it can't record, like mouse movements,
some things it doesn'r record. Some things would be
very complicated, like recording operations on the
selection and translating it to ranges...

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Keep your VBA code safe,
sign the ClassicVB petition www.classicvb.org
 
K

Klaus Linke

Another option would be the built-in bookmark \HeadingLevel:
Selection.GoTo What:=wdGoToBookmark, Name:="\HeadingLevel"

Regards,
Klaus
 
V

VespersOf1610

Klaus said:
Another option would be the built-in bookmark \HeadingLevel:
Selection.GoTo What:=wdGoToBookmark, Name:="\HeadingLevel"

That does exactly what I want. Thanks for the tip Klaus!

Regards,
Tom Davey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top