• Creating an index referencing line numbers

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Creating an index referencing line numbers

    Author
    Topic
    #353147

    This is a pretty obscure request. May some of you will
    view it as a challenge.

    I have a document which has each line numbered (using Word’s line numbering feature). I want to create an index for the document, but instead of the index showing the page numbers that the indexed word appears on, I want the index to show the line numbers.

    Can Word do this for me (perhaps with some help from Visual Basic)? Otherwise, I’ll have to do all of it the manual way.

    Thanks for your suggestions and interest!

    Viewing 0 reply threads
    Author
    Replies
    • #515782

      Yes it’s a pretty obscure request.
      Yes it’s an interesting challenge.
      Yes Word can do this for you (with some help from VBA).
      Here’s how:

      When you mark your index entries in the document (I don’t know much about Indexes, so I’m assuming this is done by means of the Mark Index Entry dialog which can be accessed via Insert>Index and Tables etc.):

      Under Options click on the Cross-Reference option button.
      Replace the default text (“See”) with “xln” (that’s lower case XLN) – this is a marker for the macro to find later.

      Then before inserting the Index proper, run the following macro – this looks for the “xln” reference in each XE (Index) field, and replaces it with the line number for the start of the marked text.

      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
      

      PS: close readers will note that I found a quick use for the Find/Replace Do..Loop which Phil R. shot down last night.

      • #515831

        Nice! Can I steal^H^H^H^H^H borrow this for my Indexer tool and give you credit?

        • #515920

          Sounds OK to me.

          Here’s a Lionel Trilling quote which Ken Getz used at the start of the foreword of his VBA Developer’s Handbook:

          “Immature artists imitate; mature artists steal.”

          Maybe nowadays this would be called “appropriation”. Adds an additional layer of meaning to “found objects”, eh?

          • #515971

            >”found objects”

            This sounds like a reference to something I ought to know in VB/VBA.

            What is a “found object” in our environment? Does it have a meaning?

            • #515997

              Nah, these were art world buzzwords maybe 20-30 years ago (don’t know too much about this) – “found objects” were objects the artist found in his environment and incoporated into his art. Extending this from physical artifacts to cultural ones, a found object could be some element of for instance, mass culture, which the artist could ‘appropriate’ and reuse in a context which commented on its original cultural meaning blah blah blah.

              I was just playing around with this and the idea of appropriating code objects (particularly ones that deal with the “Find Object”)….

            • #515998

              *** Geoff W URL Added ***

              A recent example being Bar Tabs ([Post#: 17072 / re: 16673 ] )

              Andrew C

      • #515947

        Hi Gary:

        >>PS: close readers will note that I found a quick use for the Find/Replace Do..Loop which Phil R. shot down last night.<<

        Must be another Phil R. This onewould never shoot down one of your ideas.

      • #516799

        Thanks so much for your help! I’ve been down with the
        flu since the post, so haven’t had a chance to try it.

        Thanks again!

    Viewing 0 reply threads
    Reply To: Creating an index referencing line numbers

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: