How to insert page break into each page

R

Richard S.

How do I page through a document to detect each page that does not have a
page break, and then insert a page break into any page that has none?
 
J

Jean-Guy Marcil

Richard S. was telling us:
Richard S. nous racontait que :
How do I page through a document to detect each page that does not
have a page break, and then insert a page break into any page that
has none?

Page Breaks may create all kinds of undesired side effects, especially if
you are blindly inserting them with VBA.

Instead, you might use the "Page Break Before" feature of paragraphs.

The only problem is that it you have paragraphs running from the bottom of
a page to the top of the following page, the whole paragraph will be brought
to the top of the following page.
To prevent this, you would need a lot more code...

Dim rgePage As Range
Dim rgeSave As Range


Set rgeSave = Selection.Range
Set rgePage = ActiveDocument.Range.Paragraphs(1).Range

With rgePage
.End = .Bookmarks("\Page").Range.End + 1
Do While .End < ActiveDocument.Range.End - 1
.Collapse wdCollapseEnd
.Paragraphs(1).Range.ParagraphFormat.PageBreakBefore = True
.Select
.End = .Bookmarks("\Page").Range.End + 1
Loop
End With

rgeSave.Select

However, if you have a fairly simple document and feel that you won't have
any problems with page breaks (I never use them!), try this slightly
modified version:

Dim rgePage As Range
Dim rgeSave As Range


Set rgeSave = Selection.Range
Set rgePage = ActiveDocument.Range.Paragraphs(1).Range

With rgePage
.End = .Bookmarks("\Page").Range.End
Do While .End < ActiveDocument.Range.End - 1
.Collapse wdCollapseEnd
.InsertBreak wdPageBreak
.Select
.End = .Bookmarks("\Page").Range.End
Loop
End With

rgeSave.Select
 

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