Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditionaly placing page braks in Word VBA. 1

Status
Not open for further replies.

Ruairi

Programmer
May 14, 2000
314
0
0
US
I am using a VBA macro in Word 2002 (office XP) to format a document. One of the things i want to do is place a page
break before the line "PACIFIC RIM LOG SCALING BUREAU, INC
". I only want to put a page break before it if it is NOT the first thing on the page. For example, in the text below
i would want to put " AVG DIA/LEN 12.8/13.7 12.2/12.5" on it's own page. I have code to break before each occurence of the line, but i can't figure out how to test for it not being the first line on a page, as it is i'm getting a lot of totally blank pages before the line.


AVG DIA/LEN 12.8/13.7 12.2/12.5
.
PACIFIC RIM LOG SCALING BUREAU, INC
.
SCALE CERTIFICATE


Here is the code i'm using to break before the line:
<code>
Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=1


SearchAgain:
Selection.Find.ClearFormatting
With Selection.Find
.Text = &quot;PACIFIC RIM LOG SCALING&quot;
.Replacement.Text = &quot;&quot;
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
If FirstPage = False Then
Selection.HomeKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine, Count:=1
Else
FirstPage = False
End If
GoTo SearchAgain
End If
</code>


Any ideas? TIA
Ruairi Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
may not be what you are looking for but...

Sub test()
Dim oPara As Paragraph

For Each oPara In ActiveDocument.Paragraphs
If InStr(1, oPara.Range.Text, &quot;PACIFIC RIM LOG SCALING&quot;, vbTextCompare) Then
oPara.PageBreakBefore = True
End If
Next oPara

Set oPara = Nothing
End Sub
 
Try something like:

Sub ConditionalPageBreak(i_strText As String)

Dim lngPage As Long

'find text
With Selection
With .Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:=i_strText
End With

'check for page change
If .Find.Found Then
.Collapse wdCollapseStart
lngPage = .Information(wdActiveEndPageNumber)
.MoveUp wdLine

If .Information(wdActiveEndPageNumber) = lngPage Then
'insert page break
.MoveDown wdLine
.InsertBreak wdPageBreak

End If
End If

End With

End Sub

M ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top