+ Reply to Thread
Results 1 to 9 of 9

How to determine in VBA the Heading-X styled heading just above the selection?

  1. #1
    Registered User
    Join Date
    04-19-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    6

    How to determine in VBA the Heading-X styled heading just above the selection?

    I have a Word document with multi-level headings -- table of contents, styles Heading 1-n, all that. When I pull up the Navigation Pane and move the text cursor within the document, the Navigation Pane highlights the heading closest to the cursor position. Isn't there some way get what that heading is in VBA -- some property of the Range or Selection object? Thanks.
    Last edited by jjasmith4; 04-19-2019 at 05:40 PM.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to determine in VBA the Heading-X styled heading just above the selection?

    the VBA sytax for returning the selected cell is 'Selection"
    Please Login or Register  to view this content.
    Last edited by JLGWhiz; 04-19-2019 at 04:27 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: How to determine in VBA the Heading-X styled heading just above the selection?

    JLGWhiz: Your reply has nothing to do with the OP's question.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    Registered User
    Join Date
    04-19-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    6
    Quote Originally Posted by macropod View Post
    JLGWhiz: Your reply has nothing to do with the OP's question.
    Umm, this is in Word, not Excel. FYI use vbCrlf in messageboxes, not vbLf.

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: How to determine in VBA the Heading-X styled heading just above the selection?

    Try:
    MsgBox Selection.Range.GoTo(What:=wdGoToHeading, Which:=wdGoToPrevious).Paragraphs.First.Range.Text
    or:
    MsgBox Selection.Bookmarks("\HeadingLevel").Range.Paragraphs.First.Range.Text
    Last edited by macropod; 04-21-2019 at 06:04 PM.

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: How to determine in VBA the Heading-X styled heading just above the selection?

    Cross-posted at: https://stackoverflow.com/questions/...-the-selection
    Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html

  7. #7
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to determine in VBA the Heading-X styled heading just above the selection?

    Quote Originally Posted by macropod View Post
    JLGWhiz: Your reply has nothing to do with the OP's question.
    @macropod
    Guess I need to start reading these a litrtle closer.

  8. #8
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to determine in VBA the Heading-X styled heading just above the selection?

    Quote Originally Posted by jjasmith4 View Post
    Umm, this is in Word, not Excel. FYI use vbCrlf in messageboxes, not vbLf.
    vbLf will work also, unless you have a carraige on your system that needs to return.

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: How to determine in VBA the Heading-X styled heading just above the selection?

    Quote Originally Posted by JLGWhiz View Post
    Quote Originally Posted by jjasmith4 View Post
    Umm, this is in Word, not Excel. FYI use vbCrlf in messageboxes, not vbLf.
    vbLf will work also, unless you have a carraige on your system that needs to return.
    As will vbCr

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Search Heading and copy first cell under heading
    By Simone Fick in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-19-2017, 06:21 PM
  2. Replies: 5
    Last Post: 07-26-2016, 09:16 AM
  3. [SOLVED] Determine if heading is within range of +-15 of another heading
    By magnus.blomquist in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-06-2016, 12:16 PM
  4. [SOLVED] Look for a certain heading but if not found then move to next heading you are after.
    By Steve2107 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-03-2014, 03:02 AM
  5. [SOLVED] Sub-heading stuck at same heading number
    By Hallet in forum Word Formatting & General
    Replies: 1
    Last Post: 06-01-2012, 08:16 AM
  6. [SOLVED] In a table produce an value by column heading and row heading
    By naflan in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-27-2005, 01:25 PM
  7. [SOLVED] how to determine column heading from lookup
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-25-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1