PDA

View Full Version : Find/Replace, Search Remainder = No



nisaia
10-09-2008, 11:02 AM
Hello,

I am just starting out with recording and modifying macros. I am using Word 2003. I have recorded a macro that does a Find and Replace several times for different criteria. The macro runs on text that is selected so once the Find/Replace feature reaches the end of the selection, a message box appears asking if I want to continue searching the rest of the document. Since the answer will always be no, can I add a line of code that will answer the question; thus, eliminating the appearance of the message box each time the Find/Replace code is run? If so, what would the code for this be?

fumei
10-10-2008, 02:29 PM
It may help if you posted the actual code, but as a suggestion, if this is ONLY for whatever has been selected, you could make a Range object of the Selectiin, and then do the find/replace on that. It would terminate once it reaches the end - no message.Dim r As Range
Set r= Selection.Range
With r.Find
' whatever it is you are doing

End With

nisaia
10-10-2008, 02:48 PM
Hi fumei,

Thank you for your response. Well, actually, I purposefully did not attach the code since I am such a VBA "newbie" and am somewhat embarrassed :nervous: to reveal the extent of my inexperience. But, here goes. I recorded most of this. So, as mentioned previously, I'd like it to run without the pop-up box asking if you want to continue searching at the beginning of the document each time a search/replace function is run.

Sub AAReqts()
'
' AAReqts Macro
' Macro recorded 10/7/2008 by nisaia
'
' ConvertNumberstoText
ActiveDocument.ConvertNumberstoText

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".^t"
.Replacement.Text = ". "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " """
.Font.Color = wdColorDarkBlue
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = """:^p"
.Font.Color = wdColorDarkBlue
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Font.Name = "Times New Roman"
.Font.Color = wdColorAutomatic
.Text = "^p"
.Replacement.Text = "^l^t^t"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Font.Color = wdColorDarkBlue
.Replacement.Text = ""
.Replacement.Font.Color = wdColorAutomatic
.Replacement.Font.Italic = False
.Replacement.Font.Bold = False
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l^t^t>>><<<"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace heading font Arial with Times New Roman
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Font.Name = "Arial"
.Replacement.Font.Name = "Times New Roman"
.Replacement.Font.Size = 12
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


End Sub

fumei
10-14-2008, 10:31 AM
1. Please use the VBA code tags. It makes it easier to read.

2. Again, it would be better to use Range, rather than Selection. Also, as I mentioned, it would also allow you to do your searching (find/replace) without those messages.

3. If I understand it, you have a number of different criteria you are actioning, yes?

.Text = ".^t"
.Replacement.Text = ". "

.Text = " """
.Font.Color = wdColorDarkBlue
.Replacement.Text = "^t"

.Text = "^l"
.Replacement.Text = " "

etc.

You can do that. You simply need to resize the Range object back to the Selection. You stated that it was "selected text", correct?
Dim r As Range
Set r = Selection.Range
With r.Find
.Text = ".^t"
.Replacement.Text = ". "
.Forward = True
.Execute Replace:=wdReplaceAll
End With

' reset Range object for Selection again
' and use it again
Set r = Selection.Range
With r.Find
.Text = " """
.Font.Color = wdColorDarkBlue
.Replacement.Text = "^t"
.Execute Replace:=wdReplaceAll
End With

' reset Range object for Selection again
' and use it again
Set r = Selection.Range
With r.Find

etc. etc.

nisaia
10-17-2008, 03:28 PM
Hi fumei :hi:

Thanks for your help. I think I understand. Let me try this and I'll get back to you in a few days!