View Single Post
 
Old 05-14-2016, 07:36 AM
dan88 dan88 is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2016
Posts: 24
dan88 is on a distinguished road
Default Find only the first Instance of Words -Then Do Replacement

Hello,

I would be grateful for help on my code below.

I just want to find the first instance of each words in the array and then do a replace task on it.

Example find the first instance of Air Boat then do a replacement on that only. Ignore all other Air Boats

Code:
Sub Insert_After_First_Instance()

    Dim oRng As Word.Range
    Dim arrWords
    Dim i As Long
    arrWords = Array("Air Boat", "Power Boat", "Ferry")
    arrInsertAfter = Array("AB123", "PB456", "F123")
  
For i = 0 To UBound(arrWords)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = arrWords(i)
            
            .MatchWholeWord = True
            .Replacement.Text = arrWords(i) & arrInsertAfter(i)
   
            .Execute Replace:=wdReplaceAll
        End With
    Next

End Sub
any ideas are welcome thank you

dan
Reply With Quote