Modifications

D

Designingsally

I got this sample data.

I love you. I love this world. I love this country. I love myself.

I got this find and replace macro which ll replace love to hate. when i m in
the third love in the given sentence i realise that i want to undo the
previous process (second love in the sentence)
The macro code I got uses the YES, NO, CANCEL buttons.
On hitting the CANCEL button, macro searches the word of previous instance.
If any, it changes the previous instance.

Now I want the macros to do is NOT to replace the word of previous instance
but JUST highlight the word alone and show the message box as "The
Recommended Word is and so on. Replace? with YES, NO, CANCEL button", so
that user has the discretion to replace the word or skip the word. And l also
want the macros continue the process of find and replace even after the
previous instance is found or not.

My macro code partially satisfies the process but i m unable to ask macro to
just highlight the word and then continuing to have the message as mentioned
in the above para.

Can someone help me out with this? Is it possible to modify?

Thanks in advance

Dim orng As Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
Dim boolChanged As Boolean

sFindText = "love" 'the word to find
sRepText = "hate" 'the word to replace
boolChanged = False

With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute(findText:=sFindText)
Set orng = Selection.Range
sRep = MsgBox("The Recommended Word is and so on. Replace?",
vbYesNoCancel)
If sRep = vbCancel Then
If boolChanged Then
ActiveDocument.Undo
Exit Sub
Else
MsgBox "You cannot undo the last change as there was no
last change."
Exit Sub
End If
End If
If sRep = vbYes Then
orng.Text = sRepText
boolChanged = True
End If
Wend
End With
End With
 
D

Designingsally

ok I was working on it. So i m able to achieve 90% of what i want. But i m
still unable to highlight the text of previous instance. I hops some help
comes along.


Dim orng As Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
Dim boolChanged As Boolean

sFindText = "love" 'the word to find
sRepText = "hate" 'the word to replace
boolChanged = False

With Selection
.HomeKey wdStory
With .FInd
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute(findText:=sFindText)
Set orng = Selection.Range
sRep = Msgbox("The Recommended Word is and so on. Replace?",
vbYesNoCancel)
If sRep = vbCancel Then
If boolChanged Then

Set orng = Selection.Range
sRep = Msgbox("The Recommended Word is and so on. Replace?",
vbYesNoCancel)
ActiveDocument.Undo
Else
Msgbox "You cannot undo the last change as there was no
last change."
Exit Sub
End If
End If
If sRep = vbYes Then
orng.Text = sRepText
boolChanged = True
End If
Wend
End With
End With
 
G

Greg Maxey

This is really cobbled up, but seems to do basically what you described. It
would likely be far more complex is you wanted to back up more than one
step. I suggest if you feel you may need to change your mind then when you
are done run a similar macro that looks for hate and replaces with love.

Sub ScratchMacro()
Dim orng As Range
Dim oBMRange As Range
Dim sFindText As String
Dim sRepText As String
Dim boolChanged As Boolean
sFindText = "love" 'the word to find
sRepText = "hate" 'the word to replace
boolChanged = False
Set orng = ActiveDocument.Range
Err_ReEntry:
With orng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindStop
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute(findText:=sFindText)
orng.Select
Select Case MsgBox("The Recommended Word is and so on. Replace?",
vbYesNoCancel)
Case vbCancel
If boolChanged Then
ActiveDocument.Bookmarks("LastChange").Range.Select
On Error GoTo Err_Handler
Err.Raise 1000
On Error GoTo 0
Else
MsgBox "You cannot undo the last change as there was no last
change."
End If
Case vbYes
orng.Text = sRepText
ActiveDocument.Bookmarks.Add "LastChange", orng
boolChanged = True
orng.Collapse wdCollapseEnd
Case vbNo
orng.Collapse wdCollapseEnd
End Select
Loop
End With
Err_Handler:
If Err.Number = 1000 Then
If MsgBox("Do you want to change this back to ""love""", vbYesNo, "Change
back?") = vbYes Then
Set oBMRange = ActiveDocument.Bookmarks("LastChange").Range
oBMRange.Text = "love"
ActiveDocument.Bookmarks.Add "LastChange", oBMRange
End If
Set orng = ActiveDocument.Range
orng.Start = ActiveDocument.Bookmarks("LastChange").Range.Start
Resume Err_ReEntry
End If
End Sub
 
D

Designingsally

Thanks Greg. It help me a lot. The code was like a life saver to me.
I m further using the code to intergrate it with User forms as mentioned in
your website.
 

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