Find and Replace a macro

D

Designingsally

Sub Demo()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "find"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found = True Then
MsgBox ("hi"), 45
.Replacement.Text = "fine"
.Execute Replace:=wdReplaceAll
End If
.Text = "many"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found = True Then
MsgBox ("hgh"), 45
.Replacement.Text = "uses"
.Execute Replace:=wdReplaceAll
End If
End With
End Sub
Hi

Take a look at the code above. Once the word find is found it is replaced as
fine. But only in the first instance the Message box is shown and the further
instances when the word FIND is found it is getting automatically replaced.
Which I dont want to happen. If a document has 10 finds, I want each find to
be highlighted and msgbox to be displayed at each case ie 10 times. I want
the macros to do it sequentially one by one. Can someone send me the code or
direct me how to do that?

And in the messgae box i want the 2 buttons ACCEPT and DECLINE. If the user
clicks ACCEPT the word must be autochanged. If the user clicks DECLINE I want
macros to be skipped to the next FIND in the document.

I m a novice in programming and macros. Pls help me thru this. Thanks a ton
in advance.
U can also contact me in (e-mail address removed)
 
G

Graham Mayor

Try

Sub Demo()
Dim orng As Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
sFindText = "Find" 'the word to find
sRepText = "Replaced" 'the word to replace
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute(findText:=sFindText)
Set orng = Selection.Range
sRep = MsgBox("Replace?", vbYesNo)
If sRep = vbYes Then
orng.Text = sRepText
End If
Wend
End With
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top