Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    What is my active cursor position's heading?

    Hi!,
    I want to figure out what is my active cursor position's heading?

    Heading1
    Heading2
    Heading3a
    Bla bla bla bla bla bla bla.
    Bla bla bla bla bla bla bla.
    Heading3b
    Bla bla bla bla bla bla bla.
    Bla bla bla bla bla bla bla. Now I'm here...

    THE RESULT THAT I WANT TO KNOW.
    Yor active cursor position's headings are: Heading 3b & Heading2 & Heading1.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    It is possible to do this and the following link shows some code that points to how I would attempt it. I don't have the time to do the code at the moment but the concept is basically to:
    1. Use outline promote (Alt-Shift-Left arrow) on an unnumbered paragraph in order to find out what heading level is the nearest preceding heading.
    2. Loop backwards through the headings to find successively higher outline levels

    http://www.msofficeforums.com/word/2...-headings.html
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I tried, but couldn't get the active cursor position's headings.
    Not levels but as text.

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Try this then. It will probably fail miserably if the heading structure isn't logically arranged (ie H1 followed by H2 followed by H3 etc)

    Code:
    Sub POC()
      Dim aRng As Range, iLev As Integer
      Dim str As String
      
      Set aRng = Selection.Range
      Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1
      iLev = Selection.Paragraphs(1).OutlineLevel
      str = "H" & iLev & ": " & Selection.Paragraphs(1).Range.Text & str
      For i = iLev - 1 To 1 Step -1
        Do Until Selection.Paragraphs(1).OutlineLevel = i
          Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1
        Loop
        str = "H" & i & ": " & Selection.Paragraphs(1).Range.Text & str
      Next i
      
      MsgBox str
      aRng.Select
      
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Perfect!
    Thank you very much.

Posting Permissions

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