Consulting

Results 1 to 5 of 5

Thread: Creating a Word index for a pre-existing list of names

  1. #1

    Creating a Word index for a pre-existing list of names

    Is there a way of feeding Word with a list of names/strings and producing an index of the page numbers of where the names/strings appear? I can't see that there is without laboriously adding appropriate index markers. Has anyone written the VBA code to do this?

    e.g. You feed in a list such as:

    Bill
    Fred
    Jane

    And get back:

    Bill, 2, 10, 67
    Fred, 6, 8, 21
    Jane, 123, 135, 354

    Thanks

  2. #2
    How about

    Sub Macro1()
    'Graham Mayor - https://www.gmayor.com - Last updated - 04 Feb 2019
    Const strList As String = "Bill|Fred|Jane"
    Dim Coll As Collection
    Dim oRng As Range
    Dim vName As Variant
    Dim i As Integer, j As Integer
        vName = Split(strList, "|")
        For i = 0 To UBound(vName)
            Set Coll = New Collection
            Set oRng = ActiveDocument.Range
            oRng.End = ActiveDocument.Range.Paragraphs(ActiveDocument.Range.Paragraphs.Count - i).Range.Start
            With oRng.Find
                Do While .Execute(vName(i))
                    Coll.Add oRng.Information(wdActiveEndPageNumber)
                Loop
            End With
            ActiveDocument.Range.InsertAfter vbCr & vName(i) & ", "
            For j = 1 To Coll.Count
                ActiveDocument.Range.InsertAfter Coll(j)
                If j < Coll.Count Then ActiveDocument.Range.InsertAfter ", "
            Next j
        Next i
    lbl_Exit:
        Set oRng = Nothing
        Set Coll = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks. That should do the trick nicely. Need only to add an interface with a Word doc that contains the several hundred names and strings and the target doc to be indexed.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use an index concordance file. No code required. See: https://www.dummies.com/software/mic...cordance-file/.

    The Index
    concordance document is essentially a two-column table in which the texts to be indexed are in the first column and the form of the Index entries are in the second column. Done properly, the process is effective, straightforward, and efficient.

    See also: https://answers.microsoft.com/en-us/...5-418fc592862b
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Great. That's also what I was looking for.

Tags for this Thread

Posting Permissions

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