I have a large Word doc with many figure references that are hard text. I'd like to update them to real cross-references. My code only replaces two instances midway through the document, and I can't figure out how to improve it.
Dim myFigs As Variant
myFigs = ActiveDocument.GetCrossReferenceItems(ReferenceType:="Figure")
' Loop through all paragraphs in the document to update cross-references
For Each para In ActiveDocument.Paragraphs
If para.Style = "Body Text" Then
' Search for plain text cross-references to figures
Set crossRefRange = para.Range
Do While crossRefRange.Find.Execute(FindText:="Figure [0-9]{1,}\.[0-9]{1,}", MatchWildcards:=True)
' Store the found range
Set crossRefRange = crossRefRange.Duplicate
' Extract the figure number
figureNumber = Split(crossRefRange.Text, " ")(1)
' Check if the figure number exists in the dictionary
If figureCaptionDict.Exists(figureNumber) Then
Dim searchString As String
searchString = figureNumber
For i = LBound(myFigs) To UBound(myFigs)
If InStr(myFigs(i), searchString) > 0 Then
fig = myFigs(i)
Debug.Print figureNumber & " " & fig
crossRefRange.Select
Selection.Delete
Selection.Collapse Direction:=wdCollapseStart
Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:=wdOnlyLabelAndNumber, _
ReferenceItem:=i, InsertAsHyperlink:=True
Exit For ' Exit the loop after inserting cross-reference
End If
Next i
End If
Loop
End If
Next para