PDA

View Full Version : [SOLVED:] Creating a Word index for a pre-existing list of names



johndavidson
02-03-2019, 08:41 PM
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

gmayor
02-03-2019, 10:37 PM
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

johndavidson
02-03-2019, 11:27 PM
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.

macropod
02-03-2019, 11:32 PM
You could use an index concordance file. No code required. See: https://www.dummies.com/software/microsoft-office/word/in-word-2016-mark-index-entries-with-a-concordance-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/msoffice/forum/msoffice_word-mso_mac-mso_o365b/alphabetizing-index-in-word-2016/b22c25ec-0720-4403-97f5-418fc592862b

johndavidson
02-04-2019, 12:05 AM
Great. That's also what I was looking for.