Discussion:
how do I get the heading number of the selection?
(too old to reply)
charlieLWallace
2005-07-05 00:11:01 UTC
Permalink
I'm trying to access the section number (actually the legal-format heading
number, using auto numbering, example 1.4.3) of the selection from within a
macro. Hard to search for this because of confusion between heading numbers
and section numbers.
Jezebel
2005-07-05 07:46:38 UTC
Permalink
selection.Range.ListFormat.ListString
Post by charlieLWallace
I'm trying to access the section number (actually the legal-format heading
number, using auto numbering, example 1.4.3) of the selection from within a
macro. Hard to search for this because of confusion between heading numbers
and section numbers.
Jonathan West
2005-07-05 10:52:59 UTC
Permalink
Post by Jezebel
selection.Range.ListFormat.ListString
This will work if the cursor is actually positioned in the heading. if it is
not, then the following should work, provided that headings have been
implemented using the Heading styles

ActiveDocument.Bookmarks("\HeadingLevel").Range. _
Paragraphs(1).Range.ListFormat.ListString

This will get the heading number of the next heading above the current
selection.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jezebel
2005-07-05 12:19:14 UTC
Permalink
Works for me, Jonathon. Rather surprisingly, perhaps. Try it :)
Post by Jonathan West
Post by Jezebel
selection.Range.ListFormat.ListString
This will work if the cursor is actually positioned in the heading. if it
is not, then the following should work, provided that headings have been
implemented using the Heading styles
ActiveDocument.Bookmarks("\HeadingLevel").Range. _
Paragraphs(1).Range.ListFormat.ListString
This will get the heading number of the next heading above the current
selection.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
charlieLWallace
2005-07-06 04:51:01 UTC
Permalink
Hi,
Hmmm. This seems to work only when I'm at heading level one - otherwise
returns blank! Here's how I implemented your suggestion: The MsgBox line is
split below, but is all one line in my macro.

Sub genBookmarkList()
MsgBox
ActiveDocument.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListString
Exit Sub

When I run this macro with the cursor in the text or heading of my section
14, it returns "14.", but when I put the cursor in the text or heading of
section 14.2, I get nothing. I was hoping to get "14.1" or "14.1.".

Suggestions?

Thanks for your help!!!!!!!!!!!!!!

-- Charlie
Post by Jonathan West
Post by Jezebel
selection.Range.ListFormat.ListString
This will work if the cursor is actually positioned in the heading. if it is
not, then the following should work, provided that headings have been
implemented using the Heading styles
ActiveDocument.Bookmarks("\HeadingLevel").Range. _
Paragraphs(1).Range.ListFormat.ListString
This will get the heading number of the next heading above the current
selection.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Cindy M -WordMVP-
2005-07-05 08:03:35 UTC
Permalink
Hi =?Utf-8?B?Y2hhcmxpZUxXYWxsYWNl?=,
Post by charlieLWallace
I'm trying to access the section number (actually the legal-format heading
number, using auto numbering, example 1.4.3) of the selection from within a
macro. Hard to search for this because of confusion between heading numbers
and section numbers.
Can you identify it by the style used?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
Roderick O'Regan
2005-07-05 23:48:37 UTC
Permalink
Thanks a lot folks. I learnt a lot from this posting. Something I
wanted to do a couple of weeks ago. Ended up at that time doing it how
Cindy suggested.

In this case here, I played around with both Jezebel's idea and a
little bit of Jonathan's and came up with the following:

ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
myHeading=Paragraphs(1).Range.ListFormat.ListString

Created a numbered Heading 1. Stuck a few paragraphs of Normal text
after it. Put my cursor anywhere in those Normal formatted paragraphs
and...

Bingo! Works like a dream. Might have to put a 'Collapse" command in
there somewhere...

Regards

Roderick

On Tue, 05 Jul 2005 10:03:35 +0200, Cindy M -WordMVP-
Post by Cindy M -WordMVP-
Hi =?Utf-8?B?Y2hhcmxpZUxXYWxsYWNl?=,
Post by charlieLWallace
I'm trying to access the section number (actually the legal-format heading
number, using auto numbering, example 1.4.3) of the selection from within a
macro. Hard to search for this because of confusion between heading numbers
and section numbers.
Can you identify it by the style used?
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
charlieLWallace
2005-07-06 05:07:03 UTC
Permalink
Thanks!
For some reason I get a syntax error when I try your code, on the
Paragraphs(1) part of line 4 below. ("Sub or function not defined")' Here's
what I tried:

Sub test()
Dim sectionNumStr As String
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
sectionNumStr = Paragraphs(1).Range.ListFormat.ListString
MsgBox sectionNumStr
End Sub

Not sure what I did wrong...

Anyway, I think this would work only at heading level 1, based on behavior
of the following sub based on Jonathan's posting (the msgbox line is split
below but is really all one line). This gives the heading level correctly
only when at heading level 1, with the cursor in the heading or the body
text, otherwise returns blank:

Sub genBookmarkList()
MsgBox
ActiveDocument.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListString
End Sub

Perhaps you could try your approach at levels 2 or higher? Thanks a million,
this stuff is sure confusing...
Post by Roderick O'Regan
Thanks a lot folks. I learnt a lot from this posting. Something I
wanted to do a couple of weeks ago. Ended up at that time doing it how
Cindy suggested.
In this case here, I played around with both Jezebel's idea and a
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
myHeading=Paragraphs(1).Range.ListFormat.ListString
Created a numbered Heading 1. Stuck a few paragraphs of Normal text
after it. Put my cursor anywhere in those Normal formatted paragraphs
and...
Bingo! Works like a dream. Might have to put a 'Collapse" command in
there somewhere...
Regards
Roderick
On Tue, 05 Jul 2005 10:03:35 +0200, Cindy M -WordMVP-
Post by Cindy M -WordMVP-
Hi =?Utf-8?B?Y2hhcmxpZUxXYWxsYWNl?=,
Post by charlieLWallace
I'm trying to access the section number (actually the legal-format heading
number, using auto numbering, example 1.4.3) of the selection from within a
macro. Hard to search for this because of confusion between heading numbers
and section numbers.
Can you identify it by the style used?
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
charlieLWallace
2005-07-06 05:23:03 UTC
Permalink
This works! Thanks for the help, folks...

Sub test()
Dim sectionNumStr As String
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
Selection.Collapse Direction:=wdCollapseStart
MsgBox Selection.Range.ListFormat.ListString
End Sub
Post by charlieLWallace
Thanks!
For some reason I get a syntax error when I try your code, on the
Paragraphs(1) part of line 4 below. ("Sub or function not defined")' Here's
Sub test()
Dim sectionNumStr As String
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
sectionNumStr = Paragraphs(1).Range.ListFormat.ListString
MsgBox sectionNumStr
End Sub
Not sure what I did wrong...
Anyway, I think this would work only at heading level 1, based on behavior
of the following sub based on Jonathan's posting (the msgbox line is split
below but is really all one line). This gives the heading level correctly
only when at heading level 1, with the cursor in the heading or the body
Sub genBookmarkList()
MsgBox
ActiveDocument.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListString
End Sub
Perhaps you could try your approach at levels 2 or higher? Thanks a million,
this stuff is sure confusing...
Post by Roderick O'Regan
Thanks a lot folks. I learnt a lot from this posting. Something I
wanted to do a couple of weeks ago. Ended up at that time doing it how
Cindy suggested.
In this case here, I played around with both Jezebel's idea and a
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
myHeading=Paragraphs(1).Range.ListFormat.ListString
Created a numbered Heading 1. Stuck a few paragraphs of Normal text
after it. Put my cursor anywhere in those Normal formatted paragraphs
and...
Bingo! Works like a dream. Might have to put a 'Collapse" command in
there somewhere...
Regards
Roderick
On Tue, 05 Jul 2005 10:03:35 +0200, Cindy M -WordMVP-
Post by Cindy M -WordMVP-
Hi =?Utf-8?B?Y2hhcmxpZUxXYWxsYWNl?=,
Post by charlieLWallace
I'm trying to access the section number (actually the legal-format heading
number, using auto numbering, example 1.4.3) of the selection from within a
macro. Hard to search for this because of confusion between heading numbers
and section numbers.
Can you identify it by the style used?
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
Debra Ann
2006-05-01 14:38:02 UTC
Permalink
Charlie,

I just asked this question and then found your answer. It works great!
thanks so much.
--
Debra Ann
Post by charlieLWallace
This works! Thanks for the help, folks...
Sub test()
Dim sectionNumStr As String
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
Selection.Collapse Direction:=wdCollapseStart
MsgBox Selection.Range.ListFormat.ListString
End Sub
Post by charlieLWallace
Thanks!
For some reason I get a syntax error when I try your code, on the
Paragraphs(1) part of line 4 below. ("Sub or function not defined")' Here's
Sub test()
Dim sectionNumStr As String
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
sectionNumStr = Paragraphs(1).Range.ListFormat.ListString
MsgBox sectionNumStr
End Sub
Not sure what I did wrong...
Anyway, I think this would work only at heading level 1, based on behavior
of the following sub based on Jonathan's posting (the msgbox line is split
below but is really all one line). This gives the heading level correctly
only when at heading level 1, with the cursor in the heading or the body
Sub genBookmarkList()
MsgBox
ActiveDocument.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListString
End Sub
Perhaps you could try your approach at levels 2 or higher? Thanks a million,
this stuff is sure confusing...
Post by Roderick O'Regan
Thanks a lot folks. I learnt a lot from this posting. Something I
wanted to do a couple of weeks ago. Ended up at that time doing it how
Cindy suggested.
In this case here, I played around with both Jezebel's idea and a
ActiveDocument.Bookmarks("\HeadingLevel").Range.Select
myHeading=Paragraphs(1).Range.ListFormat.ListString
Created a numbered Heading 1. Stuck a few paragraphs of Normal text
after it. Put my cursor anywhere in those Normal formatted paragraphs
and...
Bingo! Works like a dream. Might have to put a 'Collapse" command in
there somewhere...
Regards
Roderick
On Tue, 05 Jul 2005 10:03:35 +0200, Cindy M -WordMVP-
Post by Cindy M -WordMVP-
Hi =?Utf-8?B?Y2hhcmxpZUxXYWxsYWNl?=,
Post by charlieLWallace
I'm trying to access the section number (actually the legal-format heading
number, using auto numbering, example 1.4.3) of the selection from within a
macro. Hard to search for this because of confusion between heading numbers
and section numbers.
Can you identify it by the style used?
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
Loading...