Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2006

Tip: Looking for answers? Try searching our database.

how do I get the heading number of the selection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
charlieLWallace - 05 Jul 2005 01:11 GMT
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 - 05 Jul 2005 08:46 GMT
selection.Range.ListFormat.ListString

> 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 - 05 Jul 2005 11:52 GMT
> 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.

Signature

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 - 05 Jul 2005 13:19 GMT
Works for me, Jonathon. Rather surprisingly, perhaps. Try it :)

>> selection.Range.ListFormat.ListString
>
[quoted text clipped - 7 lines]
> This will get the heading number of the next heading above the current
> selection.
charlieLWallace - 06 Jul 2005 05:51 GMT
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

> > selection.Range.ListFormat.ListString
>
[quoted text clipped - 7 lines]
> This will get the heading number of the next heading above the current
> selection.
Cindy M  -WordMVP- - 05 Jul 2005 09:03 GMT
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 :-)
Roderick O'Regan - 06 Jul 2005 00:48 GMT
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

>Hi =?Utf-8?B?Y2hhcmxpZUxXYWxsYWNl?=,
>
[quoted text clipped - 12 lines]
>This reply is posted in the Newsgroup; please post any follow question or
>reply in the newsgroup and not by e-mail :-)
charlieLWallace - 06 Jul 2005 06:07 GMT
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...

> 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
[quoted text clipped - 33 lines]
> >This reply is posted in the Newsgroup; please post any follow question or
> >reply in the newsgroup and not by e-mail :-)
charlieLWallace - 06 Jul 2005 06:23 GMT
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

> Thanks!
> For some reason I get a syntax error when I try your code, on the
[quoted text clipped - 61 lines]
> > >This reply is posted in the Newsgroup; please post any follow question or
> > >reply in the newsgroup and not by e-mail :-)
Debra Ann - 01 May 2006 15:38 GMT
Charlie,

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

Debra Ann

> This works!  Thanks for the help, folks...
>
[quoted text clipped - 70 lines]
> > > >This reply is posted in the Newsgroup; please post any follow question or
> > > >reply in the newsgroup and not by e-mail :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.