Selective Find & Replace

S

singeredel

I am trying to write code to find and replace specific text, but I would like
it to stop and highlight each instance and let me select whether or not to
replace each instance, just like it would do in a regular find and replace
text in the document screen. This is what I have so far, but it asks if you
want to make a change but doesn't actually go to the word to be changed, so
you can't see where you are in the document:

ChangePatientToClaimant:
ResponsePatientToClaimant = MsgBox("Do you want to change all instances
of word patient with claimant?", vbYesNo + vbQuestion + vbDefaultButton1,
"ChangePatientToClaimant")
If ResponsePatientToClaimant = vbYes Then
Dim oRng As Range
ResetSearch
Set oRng = ActiveDocument.Content
With oRng.Find
.Text = "patient"
.Replacement.Text = "claimant"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
While .Execute
With oRng.Find
FindAction = MsgBox("Make change?", vbYesNo +
vbQuestion + vbDefaultButton1, "Change?")
If FindAction = vbYes Then
.Execute Replace:=wdReplaceOne,
Forward:=True
Else: End
End If
End With
Wend
End With
End If

ResetSearch

Thanks for any help!
 
H

Helmut Weber

Hi,
a matter of taste and style, too.
I prefer simple solutions, like this one:

Dim oRng As Range
ResetSearch
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "und" ' German for "and"
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
While .Execute
oRng.Select
If MsgBox("Make change?") = vbOK Then
oRng.Text = "oder" ' German for "or"
End If
Wend
End With
ResetSearch
End Sub

By the way, I think, you might be surprised
by what Your code is actually doing.
Cause as far as I see, You execute a find-operation,
and if successful, You execute another find-operation,
and then a replacement, which means, you do the
actual replacement with each second (!) found word.

But I might be wrong and can't test it right now.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
H

Helmut Weber

.... and of course,
it's only an example for searching,
stopping and replacing, as it doesn't give
you a choice to say no or to stop it at all.
:-(

It didn' take your code, as there was too much
rearranging form the newsreader format to the VBE.

Hope it helps anyway.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
S

singeredel

Thank you so much! Your version works! I am not surprised that my code
"wanders" as I have no clue what I am doing -- just a lay person trying to
get something done with a lot of trial and error and almost no knowledge. I
like your "simple solutions" -- just finding it hard to get there! :)
 

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