Macros search same word again and again.Help

D

Designingsally

Hi there
I got find and replace as shown below.
The word I desire to find is dialog and replace the word with dialog box. As
I run the macro, it correctly finds the word dialog, and replaces with dialog
box. When I run it again, it again finds dialog in word "dialog box" and
replaces it with dialog . Is it possible for macro not to carry out this
process. As the word as been replaced and it is not intelligent if the macro
could change the replaced word again and again.

I ll be glad if someone could come with some code which can overcome this
code.

Thanks for helping a novice.

Sally

Sub FindAndReplace()
' FindAndReplace Macro

Dim orng As Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
sFindText = "dialog" 'the word to find
sRepText = "dialog box" '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?", vbYesNoCancel)
If sRep = vbCancel Then
Exit Sub
End If
If sRep = vbYes Then
orng.Text = sRepText
End If
Wend
End Sub
 
G

Greg Maxey

I seems like we have covered similiar ground before. If you want to avoid
finding "dialog" if it is followed by "box" then you have to build that
condition in your code. Something like this maybe:

Sub SelectiveFindAndReplace()
Dim oRng As Range
Dim oRng2 As Range
Dim pTestString
Dim pFindText As String
Dim pReplaceText As String
pFindText = "dialog" 'the word to find
pReplaceText = "dialog box" 'the word to replace
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = pFindText
.Wrap = wdFindStop
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
While .Execute
oRng.Select
Set oRng2 = oRng.Duplicate
oRng2.MoveEnd wdWord, 2
pTestString = oRng2.Text
If Not Trim(pTestString) = "dialog box" Then
Select Case MsgBox("Do you want to replace this instance?", vbQuestion
+ vbYesNoCancel, "Replace ?")
Case vbYes
oRng.Text = pReplaceText
oRng.Collapse wdCollapseEnd
Case vbCancel
Exit Sub
End Select
End If
Wend
End With
End Sub
 
D

Designingsally

Hi Greg,

Yes you have spoken abt this earlier. I have one more doubt if I were to
find " eg" and replace with for example.
If the macro finds words like alleged, region. It ll immediately replace it
right?
Must i define all these words in TrimSpace property. Or is there any other
method?

Pls share ur thoughts.


Sally
 
G

Greg Maxey

Sally,

..MatchWholeWord

I want to eat a vegetable (eg., an eggplant)

Run this macro with .MatchWholeWord set to "True"

Sub ScrtachMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "eg"
.Replacement.Text = "for example"
.MatchWholeWord = True 'Use True or False
.Execute Replace:=wdReplaceAll
End With
End Sub
 

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