Find and Replace

S

singeredel

The purpose of the following code is to find every instance of the word
"patient" and ask whether the word should be changed to "claimaint."
Everything seems to work fine except when "vbNo" is selected. At this point
the program does not cycle to the next instance of the word "patient" but
just continues to remain at the current selection point in a continual loop.
Can anyone tell me what is wrong and what further code may be necessary to
find the next instance of the word "patient?"

Thanks!

ChangePatientToClaimant:
ResponsePatientToClaimant = MsgBox("Do you want to change instances of
the word 'patient' to 'claimant'?", vbYesNo + vbQuestion + vbDefaultButton1,
"ChangePatientToClaimant")
Dim oRng As Range
ResetSearch
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "patient"
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
While .Execute
oRng.Select
ResponseMsgBox = MsgBox("Change 'patient' to 'claimant'?",
vbYesNoCancel + vbQuestion + vbDefaultButton1, "Change")
If ResponseMsgBox = vbYes Then
oRng.Text = "claimant"
ElseIf ResponseMsgBox = vbNo Then GoTo Continue
ElseIf ResponseMsgBox = vbCancel Then GoTo DeleteMarks
End If
Continue:
Wend
End With
ResetSearch
 
H

Helmut Weber

Hi Julie,
try collapsing the range to it's end in any case.
Means:
if [...] then
[...]
rDcm.collapse direction:=wdcollapseend
elseif
[...]
rDcm.collapse direction:=wdcollapseend
elseif
[...]
rDcm.collapse direction:=wdcollapseend
Endif

Greetings from Bavaria, Germany

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

singeredel

Hi Helmut,

Thanks again for your help. That indeed moved the selection to the next
incident of the word, but now it is in a continual loop of finding the word
"patient" and never finishes.

Helmut Weber said:
Hi Julie,
try collapsing the range to it's end in any case.
Means:
if [...] then
[...]
rDcm.collapse direction:=wdcollapseend
elseif
[...]
rDcm.collapse direction:=wdcollapseend
elseif
[...]
rDcm.collapse direction:=wdcollapseend
Endif

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98




The purpose of the following code is to find every instance of the word
"patient" and ask whether the word should be changed to "claimaint."
Everything seems to work fine except when "vbNo" is selected. At this point
the program does not cycle to the next instance of the word "patient" but
just continues to remain at the current selection point in a continual loop.
Can anyone tell me what is wrong and what further code may be necessary to
find the next instance of the word "patient?"

Thanks!

ChangePatientToClaimant:
ResponsePatientToClaimant = MsgBox("Do you want to change instances of
the word 'patient' to 'claimant'?", vbYesNo + vbQuestion + vbDefaultButton1,
"ChangePatientToClaimant")
Dim oRng As Range
ResetSearch
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "patient"
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
While .Execute
oRng.Select
ResponseMsgBox = MsgBox("Change 'patient' to 'claimant'?",
vbYesNoCancel + vbQuestion + vbDefaultButton1, "Change")
If ResponseMsgBox = vbYes Then
oRng.Text = "claimant"
ElseIf ResponseMsgBox = vbNo Then GoTo Continue
ElseIf ResponseMsgBox = vbCancel Then GoTo DeleteMarks
End If
Continue:
Wend
End With
ResetSearch
 
H

Helmut Weber

Hi Julie,
instead of discussing all, forgive me, mistakes and
logical twists and missing or redunandant code,
I thought, it would be better to start anew.

When using and and select the range,
it might be advisable, to avoid range at all
and use selection from the beginning.

Like this:

Sub test55555()
Dim lRsl As Long ' Result from Msgbox
Dim sQus As String ' Question
sQus = "Change 'patient' to 'claimant'?"
' vbYesNo '4
' vbQuestion ' 32
' sum = 36
lRsl = MsgBox(sQus, 36, "Patient to Claimant")

If lRsl = vbNo Then Exit Sub

ResetSearch
With Selection
.WholeStory
.Collapse
With .Find
.Text = "patient"
.MatchCase = True
.MatchWholeWord = True
While .Execute
lRsl = MsgBox(sQus, 36, "Change")
If lRsl = vbYes Then
Selection.Text = "claimant"
End If
Selection.Collapse direction:=wdCollapseEnd
Wend
End With
.WholeStory
.Collapse
End With
ResetSearch
End Sub

HTH

Greetings from Bavaria, Germany

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

singeredel

Hi Helmut,

Thanks for coming to my rescue again. I have one problem with this code. The
word to be changed is not selected, so I cannot see what word is being
addressed at any particular time. Also, can you explain what "vbYesNo 4" and
"vbQuestion 32", "sum = 36" is about? Thanks.

Julie
 
S

singeredel

Helmut,

Please ignore my last post regarding not seeing the selection. I see what
has happened. The first message box probably doesn't need to be included.
However, I would still like to know what the 36 is all about in the message
box. Thanks again for your help!
 
H

Helmut Weber

Hi Julie,

instead of
msgbox [...], vbYesNo + vbQuestion, [...]
you could write
msgbox [...], 4 + 32, [...]
and instead of that you could add the values
msgbox [...], 36, [...]

which makes a single line of code harder to read,
but because of the short lines, I think, the code
altogether is easier to understand.

From my point of view.

Greetings from Bavaria, Germany

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

Helmut Weber

....and besides that all,

note that the messagebox may appear
over the selected word and hide it.
 

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

Similar Threads


Top