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 / March 2005

Tip: Looking for answers? Try searching our database.

Help me loop!!!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Julia - 02 Mar 2005 22:29 GMT
Hi, I've read some of the loop related posts but still can't figure this out.
Can you help?  

I have a short procedure that I need to continue until it runs out of
incidents:

Sub BillClean()
   
  Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "^m" 'find page break
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Selection.Delete Unit:=wdCharacter, Count:=1
   Selection.Extend
   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = " "
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Selection.MoveLeft Unit:=wdCharacter, Count:=1
   Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

How do I add the loop?  I need it to search for a page break and then delete
the junk after it.  Thanks much!
Tony Jollans - 03 Mar 2005 01:17 GMT
Hi Julia,

Your code finds a Page Break and then deletes it along with everything up
to, but not including the next space. I assume you want to do this for every
page break in the document.

Find.Execute returns True if the Find is successful and False if it isn't;
this can be used as a loop condition.

There is no need for the second Find - you can simply extend the selection.
Also the parameters on the Selection.Delete only refer to a collapsed
selection which you don't have, so they are not needed.

Put it all together and this should do what you want:

Sub BillClean()
 
   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "^m" 'find page break
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   
   Do While Selection.Find.Execute
       Selection.MoveEndUntil " "
       Selection.Delete
   Loop

End Sub

Enjoy,
Tony

> Hi, I've read some of the loop related posts but still can't figure this out.
>  Can you help?  
[quoted text clipped - 40 lines]
> How do I add the loop?  I need it to search for a page break and then delete
> the junk after it.  Thanks much!
Julia - 03 Mar 2005 01:33 GMT
Thank you Tony!

I tried it, and it deleted lots of pages.  Actually I needed it to find the
page break, go into extend mode, search for a space and then delete what it
selects, which would be the junk at the top of the next page.  I don't
understand your code enough to fix it myself...thank you so much!

> Hi Julia,
>
[quoted text clipped - 81 lines]
> > How do I add the loop?  I need it to search for a page break and then delete
> > the junk after it.  Thanks much!
Tony Jollans - 03 Mar 2005 07:43 GMT
Hi Julia,

I based my code on what your original did. To change it as you ask, add a
line after the Execute ..

Sub BillClean()

   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "^m" 'find page break
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
 
   Do While Selection.Find.Execute = True
       Selection.Collapse wdCollapseEnd
       Selection.MoveEndUntil " "
       Selection.Delete
   Loop

End Sub

The code is fairly straightforward. After setting up the Find, it loops,
executing the Find, and continuing until that returns False. The loop itself
(now) does three  things:

a) Collapses the Selection to skip past the found page break
b) Extends the End of the Selection until (but not including) the next space
c) Deletes what is now selected.

Enjoy,
Tony

> Thank you Tony!
>
> I tried it, and it deleted lots of pages.  Actually I needed it to find the
> page break, go into extend mode, search for a space and then delete what it
> selects, which would be the junk at the top of the next page.  I don't
> understand your code enough to fix it myself...thank you so much!
Helmut Weber - 03 Mar 2005 16:24 GMT
Hi Julia,

you don't need a loop at all,
if you want to delete spaces after a manual pagebreak,
and the pagebreak, too.
(Somehow I got the impression, you don't want to really delete
the pagebreak.) But anyway:

Sub Test0894()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
ResetSearch
With rDcm.Find
  .Text = "^m {1,}"    ' US-Version
  ' .Text = "^m {1;}"  ' some other countries
  .Replacement.Text = ""
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  ' plus some more if needed
  .Execute
End With
End Sub

In case, there are section breaks,
things might get complicated.

See:
http://word.mvps.org/faqs/General/UsingWildcards.htm

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
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.