View Single Post
 
Old 05-14-2016, 11:13 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

While Paul's modification works fine, it's a very quiet Sunday morning so ... you could also use
Code:
Sub Insert_After_One_Instance()
Dim oRng As Word.Range
Dim arrWords As Variant
Dim arrInsertAfter As Variant
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
            Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
                oRng.InsertAfter arrInsertAfter(i)
                Exit Do
            Loop
        End With
    Next i
    Set oRng = Nothing
End Sub
or for two instances (or more) add a counter e.g.
Code:
Sub Insert_After_Two_Instances()
Dim oRng As Word.Range
Dim arrWords As Variant
Dim arrInsertAfter As Variant
Dim i As Long, j As Long
    arrWords = Array("Air Boat", "Power Boat", "Ferry")
    arrInsertAfter = Array("AB123", "PB456", "F123")

    For i = 0 To UBound(arrWords)
        Set oRng = ActiveDocument.Range
        j = 0
        With oRng.Find
            Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
                oRng.InsertAfter arrInsertAfter(i)
                j = j + 1
                oRng.Collapse 0
                If j = 2 Then Exit Do 'Number of instances to process
            Loop
        End With
    Next i
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote