VBA word - Create multiple bookmarks from search results

Asked

Viewed 39 times

0

I am assembling a code that searches 'words' from a list and from the results:

  • creates bookmarks for each result
  • hyperlink to the bookmarks

The search part is working, but the bookmark creation part only creates Bookmark for the last search result and consequently only makes a hyperlink.

How can I make bookmarks for all results?

Below follows what I have so far:

Sub VagueWords()

Dim StrFind As String
Dim StrRepl As Variant
Dim i As Long

StrFind = "C1010,C1020,C1040"
StrRepl = StrFind
Options.DefaultHighlightColorIndex = wdBrightGreen

    With Selection.find
    .Text = Selection
    .Replacement.Text = ""
    .Format = True
    .MatchWholeWord = True
    .MatchAllWordForms = False
    .MatchWildcards = False
    .Wrap = wdFindContinue
    .Forward = True
    For i = 0 To UBound(Split(StrFind, ","))
        .Text = Split(StrFind, ",")(i)
        .Replacement.Highlight = True
        .Replacement.Text = Split(StrRepl, ",")(i)
        .Execute Replace:=wdReplaceAll
        
    Next i
    End With
MsgBox StrRepl
       
With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:=Selection
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With
With Selection.find
         .Text = Selection
         .Replacement.Text = ""
         .Forward = False
         .Wrap = wdFindContinue
         .Format = False
         .MatchCase = False
         .MatchWholeWord = True
         .MatchWildcards = False
         .MatchSoundsLike = False
         .MatchAllWordForms = False
    End With
    Selection.find.Execute
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
        SubAddress:=Selection, ScreenTip:="", TextToDisplay:=Selection

End Sub
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.