Can macros be used to UNDO ?

D

Designingsally

Hi sorry for posting a blank post. i pressed POST without my knowledge.

I was wondering if macros can do go one step back at the middle of a
process. for example. I got this sample data like

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 hate in the given sentence i realise that i want to undo the
previous process. In other words go back to second love in the sentence. And
after doing appropriate changes the macros ll move on to the third love is
that possible? I kmow i need to set some counter which ll enable to move
back. Can someone tell me how to do that?
Probably when the user clicks cancel button the macro ll go to the previous
process

I ll be glad if some help comes along. Thanks in advance for ur support
The macro I got is

Dim orng As Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
sFindText = "love" 'the word to find
sRepText = "hate" 'the word to replace
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
** I know that i need to add that coutner here** correct me if i m wrong.
Exit Sub
End If
If sRep = vbYes Then
orng.Text = sRepText
End If
Wend
 
J

Jean-Guy Marcil

Designingsally was telling us:
Designingsally nous racontait que :
Hi sorry for posting a blank post. i pressed POST without my
knowledge.

I was wondering if macros can do go one step back at the middle of a
process. for example. I got this sample data like

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 hate in the given sentence i realise that i want to
undo the previous process. In other words go back to second love in
the sentence. And after doing appropriate changes the macros ll move
on to the third love is that possible? I kmow i need to set some
counter which ll enable to move back. Can someone tell me how to do
that?
Probably when the user clicks cancel button the macro ll go to the
previous process

I ll be glad if some help comes along. Thanks in advance for ur
support
The macro I got is

Dim orng As Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
sFindText = "love" 'the word to find
sRepText = "hate" 'the word to replace
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
** I know that i need to add that coutner here** correct me if i m
wrong. Exit Sub
End If
If sRep = vbYes Then
orng.Text = sRepText
End If
Wend

Something like this?

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

Jean-

Thanks. It kinda helped me. But I m wondering if I could change CANCEL into
BACK done.

And I noticed that when click CANCEL. If the word is previously found, its
bring replaced. I dont want that to help, I just want the macros to highlight
the previous instance of the word. And then let the user click yes or no or
cancel as he or she wishes. Is that possible to do?

i ll be glad if u helpe me out.
 
D

Designingsally

Hi
Thanks I thought I ll repeat the post with further details so that it cab
useful for other users

I want the macros 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.

Macro code partially satisfies the process but i m unable to ask macro not
to replace the word instead 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?
The sample data would be
I love you. I love this world. I love this country. I love myself.
Thanks for the help 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
 

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