how do I get the heading number of the selection?

  • Thread starter charlieLWallace
  • Start date
C

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.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?Y2hhcmxpZUxXYWxsYWNl?=,
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 :)
 
J

Jonathan West

Jezebel said:
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
 
R

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
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
 
C

charlieLWallace

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
 
C

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
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...
 
C

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
 
D

Debra Ann

Charlie,

I just asked this question and then found your answer. It works great!
thanks so much.
 
Top