none
Print "Heading" on the "line number" line or nearby RRS feed

  • Question

  • Hello, does anyone have a VBA program that will capture and print the closest "Heading" value on or near the line number line?

    I have a bunch of sequential hyperlinks for a word and as i'm hyperlinking forward, i'd like to know under which "Heading" i'm under instead of having to scroll up to find out.

    Might anyone have a VBA program that will capture the nearest "Heading" value and print it on or near the page number line please?

    Thanks very much

    Here is my bookmarking code if anyone needs it:

    Sub BookmarkSelection()
    Dim StrFnd As String, bDirection As Boolean
    Dim fndtotcnt As Integer
    Dim fndloopcnt As Integer
    Dim frstlineno As Integer
    Dim frstpageno As Integer
    Dim exitWhile As String
    Dim SelRangeOrig As Range

    exitWhile = "n"
    fndtotcnt = 0
    fndloopcnt = 0
    StrFnd = Trim(Selection.Text)

    With Selection.Find
       .Text = StrFnd
       .ClearFormatting
       .Replacement.ClearFormatting
       .Replacement.Text = vbNullString
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
    End With

    Dim fndcnt As Integer
    Dim fndcntP1 As Integer
    fndcnt = 1
    fndcntP1 = 2
    lineno = Selection.Information(wdFirstCharacterLineNumber)
    pageno = Selection.Information(wdActiveEndAdjustedPageNumber)
    If fndcnt = 1 Then
       frstlineno = lineno
       frstpageno = pageno
    End If
    tStrFnd = Trim(StrFnd)
    sfndcnt = Str(fndcnt)
    sfndcntP1 = Str(fndcntP1)
    tsfndcnt = Trim(sfndcnt)
    tsfndcntP1 = Trim(sfndcntP1)
    bm = tStrFnd + tsfndcnt
    bm = Replace(bm, " ", "_")
    bm = Trim(bm)
    bmP1 = tStrFnd + tsfndcntP1
    bmP1 = Replace(bmP1, " ", "_")
    bmP1 = Trim(bmP1)

    With ActiveDocument.Bookmarks
        .Add bm, Selection.Range
    End With
    With ActiveDocument.Hyperlinks
        .Add Anchor:=Selection.Range, _
         SubAddress:=bmP1, _
         ScreenTip:=bm
    End With

    While Selection.Find.Execute
        lineno = Selection.Information(wdFirstCharacterLineNumber)
        pageno = Selection.Information(wdActiveEndAdjustedPageNumber)
        If fndcnt > 1 And lineno = frstlineno And pageno = frstpageno Then
           exitWhile = "y"
          'MsgBox ("EXIT" + Str(lineno) + Str(pageno) + "fndcnt:" + Str(fndcnt))
        End If
        fndcnt = fndcnt + 1
        fndcntP1 = fndcnt + 1
        If exitWhile = "n" Then
            tStrFnd = Trim(StrFnd)
            sfndcnt = Str(fndcnt)
            sfndcntP1 = Str(fndcntP1)
            tsfndcnt = Trim(sfndcnt)
            tsfndcntP1 = Trim(sfndcntP1)
            bm = tStrFnd + tsfndcnt
            bm = Replace(bm, " ", "_")
            bm = Trim(bm)
            bmP1 = tStrFnd + tsfndcntP1
            bmP1 = Replace(bmP1, " ", "_")
            bmP1 = Trim(bmP1)
            With ActiveDocument.Bookmarks
                .Add bm, Selection.Range
            End With
            With ActiveDocument.Hyperlinks
                .Add Anchor:=Selection.Range, _
                 SubAddress:=bmP1, _
                 ScreenTip:=bm
             End With
        End If
       'MsgBox ("here" + Str(lineno) + Str(pageno) + "fndcnt:" + Str(fndcnt))
    Wend
    End Sub

     

    Wednesday, June 22, 2011 12:55 AM

Answers

  • Hi bob

    About "printing" the heading information near a line number - that may not be so simple. But let's start with seeing whether we can get that Heading information.

    So, I assume as you "hyperlink forward" the Selection is changing - we can work with the Selection object? In that case, see if this bit of test code gives you the correct information:

      Sub GetHeadingInfoForSelection()
        Debug.Print Selection.Range.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.Text
      End Sub


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 22, 2011 12:46 PM
    Moderator
  • Yes Cindy thanks very much that seems to get the Heading for the Selection and i think i can incorporate that code into the process, so thanks again very much and have  a great week!

    bk

    • Marked as answer by bobk544 Wednesday, June 22, 2011 4:46 PM
    Wednesday, June 22, 2011 4:46 PM

All replies

  • Hi bob

    About "printing" the heading information near a line number - that may not be so simple. But let's start with seeing whether we can get that Heading information.

    So, I assume as you "hyperlink forward" the Selection is changing - we can work with the Selection object? In that case, see if this bit of test code gives you the correct information:

      Sub GetHeadingInfoForSelection()
        Debug.Print Selection.Range.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.Text
      End Sub


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 22, 2011 12:46 PM
    Moderator
  • Yes Cindy thanks very much that seems to get the Heading for the Selection and i think i can incorporate that code into the process, so thanks again very much and have  a great week!

    bk

    • Marked as answer by bobk544 Wednesday, June 22, 2011 4:46 PM
    Wednesday, June 22, 2011 4:46 PM