Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Jan 2001
    Location
    Lafayette, Indiana, USA
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Gary's index that references line numbers

    Gary Frieder was kind enough to take up the challenge to create an index for a document using line numbers instead of page numbers for the references. He even provided the VBA code to do it (post Feb 22, 2001). I really appreciate it!

    There is one small problem I need help with.

    This particular document has line numbering turned on. It's 90 pages long, with 1,008 lines. The trouble is, when I run Gary's code, everything works fine, except that the line numbers that the code generates (using "Selection.Information(wdFirstCharacterLineNumber) " -- see the actual code below) are the line numbers relative to the page, and not the document.

    So for example, if I'm trying to index the word "noun" at line 105 in the document, which happens to be on page 5, instead of the 105 appearing as the line number, I get the line number which corresponds to the line "noun" appears in on page 5 (for example, line 7, page 5). What I need is the 105 (the actual line number in the document).

    It seems that I just need to change the line in the code
    lngLineNum = .Information(wdFirstCharacterLineNumber)
    to something that returns the actual number in the left margin (created by turning on line numbering in the document).

    Unfortunately, I have no experience with Visual Basic. I've searched the Microsoft Visual Basic help file trying to figure out a way to get VB to return the line number which is in the left hand margin of the document, but I can't find anything.

    Any help would be greatly appreciated! To create this index for this 108 page document by hand would be tedious at best.

    Thanks so much.

    Here's Gary's code:

    Sub IndexWithLineNumbers()
    'Gary Frieder February 2001 Word 2000
    'Purpose: Enable display of line numbers for index entries
    'To set up in Document: When marking Index entries, set Option to
    ' cross-reference,and replace "See" with "xln"
    Dim objCurDoc As Document
    Dim lngLineNum As Long
    Set objCurDoc = ActiveDocument
    ActiveWindow.View.ShowHiddenText = True
    Do
    With Selection
    .HomeKey Unit:=wdStory
    With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "xln"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    !
    .MatchAllWordForms = False
    .Execute
    If .Found Then
    With Selection
    lngLineNum = .Information(wdFirstCharacterLineNumber)
    .TypeText (Str(lngLineNum))
    End With
    Else
    GoTo Exit_IndexWithLineNumbers
    End If
    End With
    End With
    Loop
    Exit_IndexWithLineNumbers:
    With ActiveWindow.View
    .ShowHiddenText = False
    .ShowAll = False
    End With
    End Sub

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Gary's index that references line numbers

    Does the document have the same number of lines on each page? If that is the case you could do this to calculate the line number (pseudocode):

    LineNumRelativeToPage + ((PageNumber - 1) * LinesPerPage)

    However, if there are a different number of lines on each page ((105 -5)/7 suggests that might be the case), then you might need to first scan through the document reading and storing the number of lines on each page. You could do that sort of like this (more pseudocode):

    GoTo Next Page
    MoveUp 1 Line
    Store PageNum and (LineNumRelativeToPage + PreviousPageCumulativeLineCount)
    Repeat Until Last Page

    Then the calculation for the document line number would be more like this (yet more pseudocode):

    LineNumRelativeToPage + PreviousLines(PageNumber - 1)

    where PreviousLines is an array of the cumulative number of lines on each page.

    I'm sure someone with just a little extra time on her/his hands could whip this up...

  3. #3
    New Lounger
    Join Date
    Jan 2001
    Location
    Lafayette, Indiana, USA
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Gary's index that references line numbers

    Thanks for your suggestion.

    You're right, there are a different number of lines per page. That's because some of the headings are different font sizes, plus paragraph spacing before and after paragraphs, etc., affects how many lines actually end up on each page.

    Your solution makes sense to me. Maybe someone who "speaks" VBA can help with the coding part. I don't even know how to get VBA to say "Hello, world."

    Thanks.

Posting Permissions

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