Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2008

Tip: Looking for answers? Try searching our database.

Looping

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brenda A. Reid - 25 Feb 2008 16:59 GMT
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 . . .
Jean-Guy Marcil - 25 Feb 2008 17:39 GMT
> 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
[quoted text clipped - 14 lines]
>     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
Brenda A. Reid - 25 Feb 2008 18:16 GMT
Thanks a mill - working great.

>> WordPerfect used to automatically quit the macro when it couldn't find
>> the
[quoted text clipped - 42 lines]
>   http://word.mvps.org/faqs/macrosvba/UsingRecorder.htm
>   http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.