Word VBA >> Insert Page Break After Selection

by MT DOJ Help Desk » Tue, 20 Jul 2004 09:35:48 GMT

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.




Word VBA >> Insert Page Break After Selection

by Jezebel » Tue, 20 Jul 2004 10:54:40 GMT


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






is





Word VBA >> Insert Page Break After Selection

by MT DOJ Help Desk » Fri, 30 Jul 2004 15:44:45 GMT

'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.
"Jezebel" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
operate
there
code
can
the




Insert Page Break After Selection

by Chad DeMeyer » Tue, 03 Aug 2004 00:27:29 GMT

ry 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


"MT DOJ Help Desk" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
with
in
break
created.
printed
collapsed
the
chance
range
your
be
object
your
for
it
in




Insert Page Break After Selection

by MT DOJ Help Desk » Wed, 04 Aug 2004 11:24:05 GMT

had,

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.
"Chad DeMeyer" <cjdemeye at bechtel dot com> wrote in message
news: XXXX@XXXXX.COM ...
the
is
at
my
page
bookmark,
the
The
if
I
a
work
AFTER




Insert Page Break After Selection

by Chad DeMeyer » Thu, 05 Aug 2004 01:01:14 GMT

lad 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


"MT DOJ Help Desk" < XXXX@XXXXX.COM > wrote in message
news:% XXXX@XXXXX.COM ...
before
problem
on
still
point,
and
things
document
then
are
In
when
better
the
it,
in
hair
in
location
that
is
Name:=BookmarkName




Similar Threads

1. change continuous page break to normal page breaks - Word VBA

2. Insert symbol selection dialogue broken in Word 2003

When I try to insert a symbol in Word 2003, the dialogue opens and displays 
the usual table of characters but clicking on them does nothing - I have to 
use the cursor keys to navigate through them.  This is NOT a printer driver 
problem (yes I tried uninstalling all of the printer drivers, although why a 
printer driver is needed to display a table of symbols is somewhat beyond 
me).  The same dialogue works perfectly in both Excel and PowerPoint.  I've 
tried 'detect and repair' with no success.

Any ideas?

3. Unable To insert Line Break when selection is in TextBox - Word VBA

4. Page Numbering restarts when inserting a Page break next page

Why does the page numbering restart to 1 when inserting a page break next 
page. This is very annoying is you insert a landscape page into a report only 
to find that the landscape page and following portrait pages have reset to 
Page 1. Is there anyway to default this to continuous?

Thanks

5. insert "Section Break-Next Page" but get "Section Break (Odd P

6. Paragraph selection and page breaks in VBA

Hi,

I'm looping through the paragraphs in a document (i.e. For Each myPara In 
ActiveDocument.Paragraphs) and looking at the style, length of paragraph and 
a number of other things. The trouble I'm having is when two paragraphs are 
separated by a page break rather than a paragraph mark: then VBA recognises 
it as only one paragraph.

Is this "expected" behaviour? I assume that it's a case of my understanding 
of a page break being different to Word's. And does anyone know of a 
(preferably) quick and easy way to overcome this so that VBA recognises it as 
two paragraphs? I could go through and insert a paragraph mark with each page 
break but I wondered if there was a more elegant solution out there...

Many thanks in advance for any help.

Nikki

7. Printing selection shows page breaks? - Word Document management

8. page break is automatically inserted when i insert a cross ref