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.
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)
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?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.