Help me loop!!!

J

Julia

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!
 
T

Tony Jollans

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
 
J

Julia

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!
 
T

Tony Jollans

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
 
H

Helmut Weber

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/
 

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