Help - Find And Replace - one word at a time

X

xxexbushpig

'Hi experts, I need your help please!
I have written a number of find and replace' macros in Word, but I need one
to interact with me and ask me every time whether or not I wish to replace
the word that has been found.
1. First, I want to run thru a word document and for every instance of "his"
ask me if I want to replace it with "her". Preferably the normal "Find and
Replace dialog box should open so I could just click on "Replace" or "Find
Next", but I can't work out how to make the box appear each time the macro
finds "his".
2. Also it would be nice if the dialog box were out of the way so I could
see the sentence in question. (but this isn't critical.)
3. Then I want to expand the macro to include a list of other words to run
the same steps - eg "him" to be replaced sometimes with "her", "mine" with
"ours" etc.
Any ideas, anyone, please?
 
T

Tony Jollans

What you want seems to be just the normal UI for Find and Replace but with
Find and Replace Texts pre-filled in. If so, something like this should do
it,

Finds = Array("Him", "Mine")
Replaces = Array("Her", "Ours")
For ndx = LBound(Finds) To UBound(Finds)
With Dialogs(wdDialogEditReplace)
.Find = Finds(ndx)
.Replace = Replaces(ndx)
.Show
End With
Next
 
X

xxexbushpig

Thank you Tony
That worked a treat - You are a star!
I had never come across LBound() and UBound() or
Dialogs(wdDialogEditReplace)before.
One small addition - it helps to leave spaces at one or the other end of the
words to be replaced, so part words are not selected.
My final macro looks like this:

Sub WordChange()
Dim Finds
Dim Replaces
Dim ndx
Finds = Array(" his ", " mine ", " him ", " me ")
Replaces = Array(" hers ", " ours", " her ", " us ")
For ndx = LBound(Finds) To UBound(Finds)
With Dialogs(wdDialogEditReplace)
.Find = Finds(ndx)
.Replace = Replaces(ndx)
.Show
End With
Next
End Sub

Thanks again
bushman
 
X

xxexbushpig

I spoke too soon!
The macro starts perfectly, but it does not continue onto the next word in
the sequence - it just continues with the first word fram the beginning of
the document again!
Does any one have any ideas, please!
 
T

Tony Jollans

When you have finished with one Find you must press Close or Cancel or
whatever it takes to end it and then the next one will be started.
 
X

xxexbushpig

Its quite a while since you helped me on this, Tony, so I hope you are still
following this thread and can help me a little further....

The macro works nicely if the word from the FINDS ARRAY that's being
searched for, still exists somewhere in the document. In that case one can
click on the "Cancel" button in the Find and Replace window, and the macro
moves onto the next word in the array. Perfect!
But, if if the word in the FINDS ARRAY being searched for does not exist in
the document, the "Cancel" button in the Find and Replace window becomes a
"Close" button. Pressing that causes an error in the macro "Run time error
5452. "Word has finished searching the document. The search item was not
found. The macro then can be ended or debugged, but not continued.
Is there a way to force it to continue?
Thanks in advance...
Bushman

Sub WordChange()
Dim Finds
Dim Replaces
Dim ndx
Finds = Array(" his ", " mine ", " him ", " me ")
Replaces = Array(" hers ", " ours", " her ", " us ")
For ndx = LBound(Finds) To UBound(Finds)
With Dialogs(wdDialogEditReplace)
.Find = Finds(ndx)
.Replace = Replaces(ndx)
.Show
End With
Next
End Sub
 
D

Doug Robbins - Word MVP

You would then have to do it this way

Dim myrange As Range
Dim Response
Finds = Array("Him", "Mine")
Replaces = Array("Her", "Ours")
For Ndx = LBound(Finds) To UBound(Finds)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:=Finds(Ndx), Forward:=True,
MatchWholeWord:=True, _
MatchCase:=True, MatchWildcards:=False, Wrap:=wdFindStop) =
True
Set myrange = Selection.Range
Response = MsgBox("Do you want to replace " & Finds(Ndx) & "
with " & Replaces(Ndx) & "?", vbYesNo)
If Response = vbYes Then
myrange.Text = Replaces(Ndx)
Else
Selection.Collapse wdCollapseEnd
End If
Loop
End With
Next


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Tony Jollans

I have come across this delayed display of alerts from dialogs from vba
before and it can be very frustrating. The easiest way round it is to ignore
errors.

If you wanted to check for the specific error you could but I'm not sure
there are any errors you particularly wouldn't want to ignore, so try this:

Sub WordChange()
Dim Finds
Dim Replaces
Dim ndx
Finds = Array(" his ", " mine ", " him ", " me ")
Replaces = Array(" hers ", " ours", " her ", " us ")
For ndx = LBound(Finds) To UBound(Finds)
With Dialogs(wdDialogEditReplace)
.Find = Finds(ndx)
.Replace = Replaces(ndx)
On Error Resume Next
.Show
On Error GoTo 0
End With
Next
End Sub
 
X

xxexbushpig

Thank you Doug
That works very well. You are a star!
I'm going to amend it to warn me when it starts a new word, though, because
the changeover is not immediately obvious.
Thanks
Bushman
 
X

xxexbushpig

Thank you Tony
I have no idea how or why that works, but it does a treat.
Very elegant programming!
Many, many thanks
Bushman
 

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