Sequentically find and replace

D

Designingsally

Hi
take a look at the code given below. Now try runnin the macro with sentence.
" Hi. I m fine. Where were you. I went to my native. I bought many dressees.
my parents are fine too. I also bought many others.
In this the macros replaces all FINE first and then MANY. But I want the
macro to change the words as can when they occur.

Can someone help me out?

Sub check()
Dim orng As Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
sFindText = "fine" 'the word to find
sRepText = "finD" 'the word to replace
With Selection
..HomeKey wdStory
With .Find
..ClearFormatting
..Replacement.ClearFormatting
..Wrap = wdFindContinue
..Format = False
..MatchCase = True
..MatchWholeWord = True
..MatchAllWordForms = False
While .Execute(findText:=sFindText)
Set orng = Selection.Range
sRep = Msgbox("Replace with finD?", vbYesNoCancel)
If sRep = vbCancel Then
Exit Sub
End If
If sRep = vbYes Then
orng.Text = sRepText
End If
Wend
sFindText = "many" 'the word to find
sRepText = "ues" '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 with uses?", vbYesNoCancel)
If sRep = vbCancel Then
Exit Sub
End If
If sRep = vbYes Then
orng.Text = sRepText
End If
Wend
End With
End With
End With
End With
End Sub
 
G

Graham Mayor

We have already been here. What you ask is not practical, but if you insist
look back to the reply that fellow MVP Greg Maxey provided last time you
asked the question.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Designingsally

Thanks. I m completely conviced with what you say :)
--
I believe in Hope.

DesigningSally


Graham Mayor said:
We have already been here. What you ask is not practical, but if you insist
look back to the reply that fellow MVP Greg Maxey provided last time you
asked the question.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi Sally,

Last week I gave you a macro that does multiple searches and replacements
and prompts for confirmation. My macro was based on your original macro,
which I modified to use an array. You then asked, as in this thread, for the
prompts to be displayed in the order that the search strings occur in the
document. Greg Maxey provided a macro based on word-by-word comparisons, and
you responded that it does what you want. It seems to me that you may have
become confused between these macros, which work very differently, and that
you went back to your original recorder-based macro to try to get it to do
everything that you want.

I think that it's great that you are making your own effort to get your code
right, but I think that you need to understand that the functionality you are
asking for requires drastic changes to the macros generated by the macro
recorder and that your macros won't look anything like the recorded macros.
For this reason, I suggest that you put your efforts into learning about the
coding elements in the macros that I and the Word MVPs give you instead of
going back each time to the recorded macro. In my opinion, this is the only
way that you will succeed, and I will be happy to explain anything that is
not clear and to point you to relevant pages in MSDN and other sources.

Meanwhile, because I think that the idea of multiple searches with prompts
that are ordered according to the location of each find in the document could
be of interest to a broader audience, I further modified my macro to handle
this ordering. This macro searches for your original search strings and can
be tested on the following text.

"The dialog is empty. The pens are in the pencil case. The sentence is
lovely."

Sub PromptToReplace()
Dim myRange As Range
Dim sRep As VbMsgBoxResult
Dim textArray As Variant
Dim resultArray As Variant
Dim i As Long
Dim j As Long
Dim pos As Long

ReDim textArray(1 To 3, 1 To 2) As String
textArray(1, 1) = "pen"
textArray(1, 2) = "pencil"
textArray(2, 1) = "lovely"
textArray(2, 2) = "bad"
textArray(3, 1) = "dialog"
textArray(3, 2) = "dialog box"
ReDim resultArray(UBound(textArray, 1) - 1) As Long
Set myRange = ActiveDocument.Range
pos = Selection.Start
myRange.Start = pos

Do While pos <> ActiveDocument.Range.End
With myRange
For i = 1 To UBound(textArray, 1)
.Start = pos
If .Find.Execute(findText:=textArray(i, 1), _
Wrap:=wdFindStop, Forward:=True) = True Then
resultArray(i - 1) = .Start
Else
resultArray(i - 1) = ActiveDocument.Range.End
End If
Next
i = 1
pos = resultArray(0)
For j = 1 To UBound(resultArray)
If resultArray(j) < pos Then
pos = resultArray(j)
i = j + 1
End If
Next
If pos <> ActiveDocument.Range.End Then
.Start = pos
.End = pos + Len(textArray(i, 1))
.Select
sRep = MsgBox("The incorrect word '" & _
textArray(i, 1) & _
"' was found in the following sentence:" _
& vbCrLf & .Sentences(1) & vbCrLf _
& vbCrLf & "Replace this word with '" & _
textArray(i, 2) & "'?", vbYesNoCancel)
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveStart Unit:=wdCharacter, Count:=1
If sRep = vbCancel Then
Exit Sub
ElseIf sRep = vbYes Then
.Text = textArray(i, 2)
End If
.End = Selection.End
.Start = Selection.Start
pos = .Start
Else
MsgBox "There is nothing more to find in the document."
End If
End With
Loop
Set myRange = Nothing
End Sub
 
D

Designingsally

Pesach

Thanks for suggesting me how I can further learn abt macros using codes
available in this discussion community.
 

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