I have the following Word 2019 macro code:
Sub CreateBibleIndex()
Dim bibleRefs() As String
Dim indexEntries() As String
Dim i As Long, j As Long
' Regular expression pattern to match Bible references
Dim regexPattern As String
regexPattern = "([1-3]?\s?[A-Z][a-z]*\.)(\s\d{1,3}[:]\d{1,3}(-\d{1,3})?)?"
' Canonical list of books of the Bible
Dim bookList() As String
bookList = Split("Genesis,Exodus,Leviticus,Numbers,Deuteronomy,Joshua,Judges,Ruth,1 Samuel,2 Samuel,1 Kings,2 Kings,1 Chronicles,2 Chronicles,Ezra,Nehemiah,Esther,Job,Psalms,Proverbs,Ecclesiastes,Song of Solomon,Isaiah,Jeremiah,Lamentations,Ezekiel,Daniel,Hosea,Joel,Amos,Obadiah,Jonah,Micah,Nahum,Habakkuk,Zephaniah,Haggai,Zechariah,Malachi,Matthew,Mark,Luke,John,Acts,Romans,1 Corinthians,2 Corinthians,Galatians,Ephesians,Philippians,Colossians,1 Thessalonians,2 Thessalonians,1 Timothy,2 Timothy,Titus,Philemon,Hebrews,James,1 Peter,2 Peter,1 John,2 John,3 John,Jude,Revelation", ",")
' Find all Bible references in the document
With ActiveDocument.range.Find
.ClearFormatting
.MatchWildcards = True
.Text = regexPattern
.Execute
While .Found
ReDim Preserve bibleRefs(i)
bibleRefs(i) = .Text
i = i + 1
.Execute
Wend
End With
' Loop through Bible references and create index entries
For i = LBound(bibleRefs) To UBound(bibleRefs)
Dim ref As String
ref = bibleRefs(i)
Dim book As String
Dim chapter As String
Dim verse As String
book = Left(ref, InStr(ref, " ") - 1)
chapter = Mid(ref, InStr(ref, " ") + 1, InStr(ref, ":") - InStr(ref, " ") - 1)
verse = Mid(ref, InStr(ref, ":") + 1)
Dim indexEntry As String
indexEntry = book & " " & chapter & ":" & verse
If Not IsInArray(indexEntry, indexEntries) Then
ReDim Preserve indexEntries(j)
indexEntries(j) = indexEntry
j = j + 1
End If
Next i
' Sort index entries in canonical order
Dim sortedEntries() As String
sortedEntries = SortIndexEntries(indexEntries, bookList)
' Create index in document
Dim indexText As String
indexText = "Index" & vbCrLf
For i = LBound(sortedEntries) To UBound(sortedEntries)
indexText = indexText & sortedEntries(i) & vbTab & "p. " & GetPageNumber(sortedEntries(i)) & vbCrLf
Next i
ActiveDocument.range.InsertAfter indexText
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
I get the error as in the heading. Cannot see how to fix. Can anyone help? Thank you