dangles1266
03-12-2014, 03:17 PM
I was asked to write a macro for someone to use in word (2010). They want to use the Find and Replace to search for the word “shall” in any style except in the style “KEEP” and replace “shall” with the word “will”. The person wants to stop at each occurrence along the way and see what happens. With this request, I want to use .Execute Replace:=wdReplaceOne, since using ReplaceOne will find, replace, and highlight the change, and let the user see what just happened. If the change is desired, then the user presses the macro button again to jump to the next occurrence and continues through the document in that manner. If they do not agree with the change, then they can press undo.
I searched online for others with the same desire to change and replace with style exceptions (including this site) and started modifying their code to match my desired outcome. I have numerous tests that have not worked. Can someone fix the code? Thanks.
Sub TEST_FND_shall_RPLC_will_EXCEPTKEEPSTYLE()
'
With Selection.Find
.Text = "shall"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
If Selection.Find.Found = True And Selection.Find.Style <> ("KEEP") Then Selection.Find.Replacement.Text = "will"
Selection.Find.Execute Replace:=ReplaceOne
End With
End Sub
I searched online for others with the same desire to change and replace with style exceptions (including this site) and started modifying their code to match my desired outcome. I have numerous tests that have not worked. Can someone fix the code? Thanks.
Sub TEST_FND_shall_RPLC_will_EXCEPTKEEPSTYLE()
'
With Selection.Find
.Text = "shall"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
If Selection.Find.Found = True And Selection.Find.Style <> ("KEEP") Then Selection.Find.Replacement.Text = "will"
Selection.Find.Execute Replace:=ReplaceOne
End With
End Sub