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