Here is my code, but it doesn’t make replace!
First I change AnyText to TEMP, after I make a cycle for replace TEMP with elements of Array TargetList, but can’t replace TEMP.
Sub First()
'
' First Macro
Dim i As Long
i = 0
Dim j As Long
Dim myWord As String
Dim msg As String
myWord = "TEMP"
TargetList = Array("AnyText", "NewWord1", "NewWord2", "NewWord3", "NewWord4")
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "AnyText"
.Replacement.Text = myWord
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next myStoryRange
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
Do While .Execute(FindText:=myWord, Forward:=True) _
= True
j = j + 1
Loop
msg = msg & "The string " & myWord & _
" found " & j & " times."
End With
msg = msg & vbCrLf & vbCrLf
MsgBox msg
Next myStoryRange
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
Do While j > -1
.Text = "TEMP"
.Replacement.Text = TargetList(i)
msg = msg & "The string " & myWord & _
" j = " & j & " i = " & i & TargetList(i) & " times."
msg = msg & vbCrLf & vbCrLf
MsgBox msg
j = j - 1
i = i + 1
If i = 5 Then
i = 0
End If
Loop
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next myStoryRange
End Sub
This code will replace any instances of “AnyText” with “TEMP”, then will replace any instances of “TEMP” with the current
(count of finds)mod 5:Note that this will change all instances of “AnyText” – even if they’re part of a larger word (for example “myAnyTextWord” will be changed to “myTEMPWord”). If you only want to search for whole words, add
.MatchWholeWord = Trueto eachWithblock.