Nesting Find and Replace Macros

L

LiCal

I need help in nesting a Find and Replace Macro into another one.


1. In the word document, the main macro is to find the string "Fri -
Sun" individually; then extend the selection to end of line.

2. the nested marco is then used to replace spaces within the line
selection with tabs.

The below nested Macro will run, but needs Ctrl-Break to stop it; I
appreciate some help in clarifying the usage of the below paramaters
for both Main/sub marcos.

---------------------------------------------
.Forward = (? true ; False)
.Wrap (?wdFindAsk ; wdFindContinue ; wdFindStop)

---------------------------------------------
is it possible to/ how to code NO for the answer to the
wdFindask (Ctrl-H) Dialog Box?

(The msg : Word has finished searching the selection. x replacements
were made. Do you wan to search the remainder of the document? Yes /
No)
=======================================



'+++++++++++++++++++++++++++

Sub MainFindReplace()
'+++++++++++++++++++++++++++


Set rDcm = ActiveDocument.Range


rDcm.Find.ClearFormatting

With Selection.Find
Ia = 0
'.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True

Do While .Execute(FindText:="Fri - Sun") = True

Ia = Ia + 1 ' Counter

' extend the selection to the end of line
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

' Nested Macro to find/replace spaces in the selection to Tabs.

SubFindReplace
Loop
End With
MsgBox "IA= " & Ia

End Sub

'++++++++++++++++++++++++++++

Sub SubFindReplace()

'++++++++++++++++++++++++++++

' Macro24 Macro
' Macro recorded 2/9/2009 by OEM
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = "^t"
.Forward = False
.Wrap = wdFindStop 'wdFindAsk ;
wdFindContinue ; wdFindStop


.Forward = False
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
'SendKeys "N"

'Resetting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With

End Sub

--
 
G

Graham Mayor

You don't need to nest macros. You need to define the found text as a range
and modify the range as required, as in the following example, which assumes
that the end of 'line' is the end of the current paragraph. Reinstate the
comment line if you don't want the spaces in the found string "Fri - Sun"
replaced also.

Dim oRng As Range
With Selection.Find
Do While .Execute(findText:="Fri - Sun", MatchWildcards:=True) =
True
Set oRng = Selection.Range
'oRng.Collapse wdCollapseEnd
oRng.MoveEnd wdParagraph
oRng = Replace(oRng.Text, Chr(32), Chr(9))
Loop
End With

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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