The How-To Geek Forums Have Migrated to Discourse


How-To Geek Forums / Microsoft Office

Find and replace macro in Word 2007

(1 post)
  • Started 3 years ago by netchie
  • Topic Viewed 1309 times

PARTNER SPOTLIGHT

Recommended: Clean Your Mac the Easy Way with CleanMyMac 3

If you want to clean up your Mac the easy way, CleanMyMac is a great tool to get the job done easily, and it will even keep your computer clean automatically.

Unlike the competition, it also includes great tools like a Clean Uninstaller, to get rid of applications and clean up the junk that they leave behind. All with the click of a button.

Download CleanMyMac 3 for Free Today

Posted yesterday 
Top
netchie
Posts: 3

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 3 years ago
Top
 



Topic Closed

This topic has been closed to new replies.