I have certain text in a word doc that is bookmarked. I would like to parse the document using Word VBA for the same words and insert a cross reference. For some reason when I insert a cross reference, the code doesn't move to the next word.
Sub ReplaceTextwithCrossRef()
Dim BMtext As String
Dim BMname As String
Dim Sel As Selection
Set Sel = Application.Selection
BMname = Sel.Bookmarks(1).Name
BMtext = Sel.Text
MsgBox BMname
MsgBox BMtext
For Each oWd In ActiveDocument.Words
oWd.Select
If oWd.Text = BMtext Then
If Selection.Bookmarks.Exists(BMname) Then
Else
Selection.InsertCrossReference ReferenceType:=wdRefTypeBookmark, _
ReferenceKind:=wdContentText, ReferenceItem:=BMname
Selection.MoveDown Unit:=wdLine, Count:=1
End If
Else
End If
Next oWd
End Sub
The user selects a bookmarked word, the code moves to the next instance of the word, and cross references it. i.e.
BOOKMARKEDITEM
WORDS1
WORDS2
BOOKMARKEDITEM
WORDS3
It will insert a cross reference on the second instance of BOOKMARKEDITEM, but it won't move to WORDS3. It gets stuck and keeps coming back to the cross reference, even if I tell it to move down with the next line of code. Any help would be appreciated.