Hi,

I have a macro where it finds specific word and replace it. The problem is sometimes the replaced word is already with the exact word so it became double words with it. Let's say I am looking for word "REPLACE" and needs to replace with colon (:) and should be "REPLACE:". Sometimes the word has already colon wiht it before using the macro which is "REPLACE:" but after using the macro, it added and changed as "REPLACED::" ( it added two colons). Can you please help me polish the VBA for me please?

Also, the more I click the macro the more it added colons so many times.

Please help!

Thanks,
n

Sub Replace()
Dim myStoryRange As Range

'First search the main document using the Selection
With Selection.Find
.Text = "Replace"
.Replacement.Text = "Repleace:"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

'Now search all other stories using Ranges
For Each myStoryRange In ActiveDocument.StoryRanges
If myStoryRange.StoryType <> wdMainTextStory Then
With myStoryRange.Find
.Text = "Replace"
.Replacement.Text = "Replace:"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "De"
.Replacement.Text = "Replace:"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
End If
Next myStoryRange

End Sub

Posted 1 year ago
Top