PDA

View Full Version : Solved: Insert Page Break Before Each Instance of String



jasoncw
06-06-2008, 08:47 AM
I wrote the below procedure to insert a page break before each found instance of a certain text string, which denotes when the next page should start.

The procedure works fine for me. What I was wondering is if there is a more efficient way to do this, or if I need to loop through the document, as I am currently doing.

The other question is for how to exit the loop. In other words, how do I know when there are no other instances of the string? I am currently using error trapping, but I was thinking there would be a better way.

Thanks for looking.

Jason

Sub DetailPSRFormat()
Application.ScreenUpdating = False

Dim Doc As Document
Set Doc = ActiveDocument

With Doc.PageSetup
.Orientation = wdOrientLandscape
.LeftMargin = InchesToPoints(0.5)
.RightMargin = InchesToPoints(0.5)
.TopMargin = InchesToPoints(0.75)
.BottomMargin = InchesToPoints(0.75)
End With
Doc.Content.Font.Size = 9

Dim EndofDoc As Long
EndofDoc = Doc.Range.End

Dim Rng As Range
'start at character 2 to avoid page break at beginning of document
Set Rng = Doc.Range(2, EndofDoc)

'insert page break for each new PSR page
Do
With Rng.Find
'text on first line of each page
.Text = "1 P"
.Forward = True
.Wrap = wdFindContinue
End With
Rng.Find.Execute

'exits loop if text is not found
On Error Resume Next
Set Rng = Doc.Range(Rng.Start - 1, Rng.Start - 1)
If Err.Number = 4608 Then Exit Do
On Error GoTo 0

Rng.InsertBreak wdPageBreak

'advance Rng.start by 2 to continue find
Set Rng = Doc.Range(Rng.Start + 2, EndofDoc)

'percent complete for status bar
Application.StatusBar = Format(Rng.Start / EndofDoc, "0%") & _
" Complete. Please wait. . ."
Loop

Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub

fumei
06-06-2008, 09:21 AM
Sub PageBefore1P()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:="1 P", _
Forward:=True) = True
r.InsertBefore (Chr(12))
r.Collapse 0
Loop
End With
End Sub

will make a manual page break before each "1 P".

jasoncw
06-06-2008, 10:15 AM
Thanks for the help, Gerry. That is definitely a little more efficient than the way I had it.

I wasn't sure whether there was a character code for a manual page break. Here are some interesting stats on elapsed time, though:

Using Replace All with Chr(12) - after document is repaginated: 1:35
Using Replace All with Chr(12) - before document is repaginated: 0:10
Using your loop above, regardless of repagination: 0:32

This is on a rather large text file (approx. 5,000 pages), and on a slower PC.

Thanks again, Gerry.

Jason

fumei
06-06-2008, 10:30 AM
Using the Find/Replace dialog (and using ReplaceAll) - as opposed to a VBA looping code - will ALWAYS be faster.

Find: 1 P
Replace: ^m1 P

Click Replace All. This replaces "1 P" with manual page break (the ^m) and "1 P". The only reason I gave a code solution is that you asked about a code solution.

Practically speaking though, again, using the Find and Replace dialog will always be faster for straightforward, well, Find and Replace.

The only advantage with a code solution is to do some logical processing during the find and replace operation.