Insert Page Break After Selection

M

MT DOJ Help Desk

Word 2000

I have the following code in a test routine that I'm developing. The code
works well as is, but just as a point of curiosity, I am wondering if it is
possible to insert a page break after the current selection, so that I can
do away with the Selection.Collapse (Down) command. I know there is a
Selection.InsertAfter command, but I was not able to get that to work in
conjunction with InsertBreak. Is there a way to rewrite the
Selection.InsertBreak command so that it inserts the page break AFTER the
current selection?

Sub TestInsertPageBreaks()

Counter = 0
For Each oBookmark In ActiveDocument.Range.Bookmarks
Counter = Counter + 1
BookmarkName = ActiveDocument.Range.Bookmarks(Counter)
If Left(BookmarkName, 6) = "Record" Then
Selection.GoTo what:=wdGoToBookmark, Name:=BookmarkName
Selection.Collapse (Down)
Selection.InsertBreak Type:=wdPageBreak
End If
Next oBookmark

End Sub

-- Tom

MT DOJ Help Desk

Making the world a safer place.
 
J

Jezebel

Not as such, but you can be more creative about how you provide the range
argument to which the Insert is applied. But a couple of comments on your
sample code:

1. Avoid using the Selection object. Pretty well everything can better be
handled directly through Range objects. Instead of finding the bookmark,
selecting its range, then operating on the selection, it's better to operate
on the bookmark range directly.

2. If you're using a For...each construction, there's no need for the
counter variable. Conversely, if you really want a counter, use a
construction like For Counter = 1 to Bookmarks.Count. As you have it, there
is potential for a very nasty bug because there's no guarantee that the
objects returned using For...each will be in index order -- so this object

ActiveDocument.Range.Bookmarks(Counter)

will not necessarily be the same as oBookmark. Not that it matters in your
sample, but it's the sort of bug that could have you tearing your hair for
days. Iterating the bookmarks collection of *document* returns them in
alphabetical order; but of a *document.range* returns them in location
order.


Simpler is:

For Each oBookmark In ActiveDocument.Range.Bookmarks
With oBookmark
If Left(.Name, 6) = "Record" Then
.Range.Collapse Direction:=wdCollapseEnd
.Range.InsertBreak Type:=wdPageBreak
End If
End With
Next
 
M

MT DOJ Help Desk

I've only recently started working with bookmarks and range objects, and
there are a few vestiges of older code still left. You're right that the
Counter is probably not needed, and I can see the advantage to working with
ranges, but I'm still figuring it all out, so sometimes there are things in
my code that aren't the best way to do things.

I tried your code, and I found that there is a problem. When the page break
is inserted, it actually *replaces* data in the document. The document is
used as a hot plate for data. We paste records into the document, then at
the end of the shift we process the records using macros that I've created.
At the end of the processing, any records remaining in the document are
printed. The page breaks are inserted to insure that each record is printed
on its own page.

So the code must be able to insert the page breaks between records. In my
original code, the bookmark is selected and then the selection is collapsed
down, and that insures that the insertion point is between records when the
page break is inserted. Your code is replacing each record with the page
break.

I'm sure there is probably an easy fix for this, but I didn't get the chance
to work on it tonight.

-- Tom

MT DOJ Help Desk

Making the world a safer place.
 
C

Chad DeMeyer

Try replacing:

If Left(.Name, 6) = "Record" Then
.Range.Collapse Direction:=wdCollapseEnd
.Range.InsertBreak Type:=wdPageBreak
End If

with:

If Left(.Name, 6) = "Record" Then .Range.InsertAfter Chr(12)

Regards,
Chad
 
M

MT DOJ Help Desk

Chad,

Thanks for the help! Your modification works.

Sorry that it took me so long to reply. My last message was the day before
I went on vacation, and while I didn't have the time to work on the problem
that day, I wanted to at least get the question posted while it was still on
my mind.

Anyway, I just tested your solution and it works great. I had see the
InsertAfter method, but was having problems using it correctly when trying
to solve this particular problem. Now that I've seen your solution, it
seems fairly obvious. I've actually used similar code in the past, but I
just didn't think of it this time. I'm still new to using ranges and still
thinking in terms of selecting the text and then moving the insertion point,
and I think that way of thinking was what was keeping me from seeing the
better solution.

-- Tom

MT DOJ Help Desk

Making the world a safer place.
 
C

Chad DeMeyer

Glad that it helped. I'm going through the same evolution myself, from
using selection more prevalently to using range whenever possible. It's
sometimes harder with a range object to envision how certain range
operations will change the shape of the range, but I'm starting to get the
hang of it.

Regards,
Chad
 

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