Insert pagebreak but ignore the first

S

Steved

Hello from Steved

Please how do I tell the macro to ignore the first instance off the text
"Race" because I would like the macro to only do the second "Race" and then
do all the next pageinserting. Thankyou.

Sub TimeShift()
Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Race"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.HomeKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine
Selection.MoveDown Unit:=wdLine
End If
Loop While Selection.Find.Found
End Sub
 
S

Steved

Hello Fumei from Steved

Fumei sometimes I might have several occurences, so using a counter is
counter productive.

You have said :I would also suggest NOT using Selection." Would you kindly
explain what you would do instead please.

Thankyou.
 
T

Tony Jollans

Most of the code you have in the loop does not need repeating. You could
rewrite it something like (pseudocode) ...

with selection.find
' set up the find
end with
do
find.execute
etc.
loop

When you have done this you can see that you can just do a preliminary
execute before the loop ... more pseudocode ...

with selection.find
' set up the find
end with
find.execute
do
find.execute
etc.
loop
 
S

Steved

Hello Tony from Steved

The below macro does not stop I had to use CTRL Break to stop it it was down
to page 875 I've only got 9 so should have stopped at page 9.

Thankyou.

Sub TestPageBreak()
With Selection.Find
Do
..Text = "Race"
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine
Loop
End With
End Sub
 
T

Tony Jollans

The below macro does not stop

That's because you never tell it to stop.

You started out with this:

Sub TimeShift()
Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Race"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.HomeKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine
Selection.MoveDown Unit:=wdLine
End If
Loop While Selection.Find.Found
End Sub

As per my suggestion you should have changed it to this:

Sub TimeShift()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "quick"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Do
Selection.Find.Execute
If Selection.Find.Found Then
Selection.HomeKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine
Selection.MoveDown Unit:=wdLine
End If
Loop While Selection.Find.Found
End Sub

The setup of the find is before the loop - as is an extra
Selection.Find.Execute.

Could you try this and see whether it meets your needs before asking further
questions about different code?
 
S

Steved

Thankyou Tony

Yes it does what is required.

Tony Jollans said:
That's because you never tell it to stop.

You started out with this:

Sub TimeShift()
Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Race"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.HomeKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine
Selection.MoveDown Unit:=wdLine
End If
Loop While Selection.Find.Found
End Sub

As per my suggestion you should have changed it to this:

Sub TimeShift()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "quick"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Do
Selection.Find.Execute
If Selection.Find.Found Then
Selection.HomeKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine
Selection.MoveDown Unit:=wdLine
End If
Loop While Selection.Find.Found
End Sub

The setup of the find is before the loop - as is an extra
Selection.Find.Execute.

Could you try this and see whether it meets your needs before asking further
questions about different code?
 

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