Looping

B

Brenda A. Reid

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 . . .
 
J

Jean-Guy Marcil

Brenda A. Reid said:
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
 
B

Brenda A. Reid

Thanks a mill - working great.


Jean-Guy Marcil said:
Brenda A. Reid said:
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
 

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