WordPerfect used to automatically quit the macro when it couldn't find the
search criteria anymore. Can Word do the same thing? Tried to do a loop
but can't get it to work. Here is my code that I want to keep looping until
it can't find anymore occurrences of *#*.

Selection.Find.ClearFormatting
With Selection.Find
.Text = "*#*"
.Forward = True
End With
Selection.Find.Execute

Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="Macrobutton nomacro [Keyboard]"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Fields.ToggleShowCodes

Tks . . .

RE: Looping by JeanGuyMarcil

JeanGuyMarcil
Mon Feb 25 09:39:01 PST 2008

"Brenda A. Reid" wrote:

> WordPerfect used to automatically quit the macro when it couldn't find the
> search criteria anymore. Can Word do the same thing? Tried to do a loop
> but can't get it to work. Here is my code that I want to keep looping until
> it can't find anymore occurrences of *#*.
>
> Selection.Find.ClearFormatting
> With Selection.Find
> .Text = "*#*"
> .Forward = True
> End With
> Selection.Find.Execute
>
> Selection.Delete Unit:=wdCharacter, Count:=1
> Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
> PreserveFormatting:=False
> Selection.TypeText Text:="Macrobutton nomacro [Keyboard]"
> Selection.MoveLeft Unit:=wdCharacter, Count:=1
> Selection.Fields.ToggleShowCodes

Try this:

Dim rgeFind As Range

Set rgeFind = ActiveDocument.Range

With rgeFind.Find
.Text = "*#*"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
With .Parent
rgeFind.Fields.Add Range:=rgeFind, Type:=wdFieldEmpty, _
Text:="Macrobutton nomacro [Keyboard]", _
PreserveFormatting:=False
End With
Loop
End With

Try not to use the Selection object. From the code you posted, I assume you
used the recorder, which always uses the Selection object. See these links
for more on taming the recorder results...
http://word.mvps.org/faqs/macrosvba/UsingRecorder.htm
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm

Re: Looping by Brenda

Brenda
Mon Feb 25 10:16:56 PST 2008

Thanks a mill - working great.


"Jean-Guy Marcil" <JeanGuyMarcil@discussions.microsoft.com> wrote in message
news:B0BAE6F5-E4DE-40B4-8A0C-29A463A758F1@microsoft.com...
> "Brenda A. Reid" wrote:
>
>> WordPerfect used to automatically quit the macro when it couldn't find
>> the
>> search criteria anymore. Can Word do the same thing? Tried to do a loop
>> but can't get it to work. Here is my code that I want to keep looping
>> until
>> it can't find anymore occurrences of *#*.
>>
>> Selection.Find.ClearFormatting
>> With Selection.Find
>> .Text = "*#*"
>> .Forward = True
>> End With
>> Selection.Find.Execute
>>
>> Selection.Delete Unit:=wdCharacter, Count:=1
>> Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
>> PreserveFormatting:=False
>> Selection.TypeText Text:="Macrobutton nomacro [Keyboard]"
>> Selection.MoveLeft Unit:=wdCharacter, Count:=1
>> Selection.Fields.ToggleShowCodes
>
> Try this:
>
> Dim rgeFind As Range
>
> Set rgeFind = ActiveDocument.Range
>
> With rgeFind.Find
> .Text = "*#*"
> .Forward = True
> .Wrap = wdFindStop
> Do While .Execute
> With .Parent
> rgeFind.Fields.Add Range:=rgeFind, Type:=wdFieldEmpty, _
> Text:="Macrobutton nomacro [Keyboard]", _
> PreserveFormatting:=False
> End With
> Loop
> End With
>
> Try not to use the Selection object. From the code you posted, I assume
> you
> used the recorder, which always uses the Selection object. See these links
> for more on taming the recorder results...
> http://word.mvps.org/faqs/macrosvba/UsingRecorder.htm
> http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm