Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retrieve heading value?

Status
Not open for further replies.

Smedowns

Technical User
Jun 22, 2009
6
US
I want to retrieve the heading that the active range is located in. For instance if I highlight a word under heading "3.2.1 Conclusions", I want a macro to assign "3.2.1 Conclusions" to a variable.

Thanks,
Smed
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ditto. Although, this is not intuitively easy. Give it a try and post what you come up with.

Just to be clear about what you wish to do:

1.1 Lsdalhdaldhaldha
1.1.1 Aldhaldhald
Adas;dj;ajdad;jad
1.2 Ajdlajd;ajd;adja;dja
Some text here
1.2.1 Yadda blah
Sghdkshdlsdj [highlight]text[/highlight] here

You want to be able - if you highlight/select the text "text" - to put "1.2.1 Yadda blah" into a string variable.

Yes?

Some hints:

1.2.1 is .Range.ListFormat.ListString
Yadda blah is .Range.Text

Two different things, and they are both properties of the paragraph:

1.2.1 Yadda blah

NOT, repeat NOT, the paragraph:

Sghdkshdlsdj [highlight]text[/highlight] here

Which is the paragraph you are using. So...you have to work backwards, finding the paragraph that is using a heading style BEFORE the paragraph that has the text you are testing. If the paragraph you are testing is not right after that heading style paragraph, say:

1.2 Ajdlajd;ajd;adja;dja
Some text here
1.2.1 Yadda blah
This is a first paragraph.
This is a SECOND PARAGRAPH.
And a third, where you are [highlight]testing[/highlight]

Then you have to iterate back, and back, until you DO get a paragraph with a ListString.

The coding itself is not horribly difficult, it is just understanding the object model. I just did it now, with three variables:

Dim r2 As Range (to be able to use the range of paragraphs)
Dim j As Long (a counter, moving backwards through paragraphs, if required)
Dim Yeah As Boolean (a boolean to set if testing paragraph range DOES have a ListString)

and 9 (nine) lines of code.


Gerry
 
Actually...thanks. I probably will not use that very much, but I made it a Function that returns the heading number and text - like "3.2.1 Conclusions" - as a string, so I can call it anywhere. It may come in handy some time (although I am not exactly sure where), so I added it to my toolbox of functions/subs.

Frankly, I am curious as to why you want to get this. What is the purpose of putting "3.2.1 Conclusions" into a string variable?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top