Inserting a page break

D

Dave Stone

I'm having problems inserting a page break in a document
I'm creating in an Access module. The document initially
is set up to contain "Test document"&vbcrlf&vbcrlf, and
using the debugger to display ActiveDocument.Range.Text
shows this very string. I then do:
ActiveDocument.Range.Collapse Direction:=wdCollapseEnd
ActiveDocument.Range.InsertBreak Type:=wdPageBreak
ActiveDocument.Range.InsertAfter vbCrLf & vbCrLf
& "This is 2nd page of text"

and after the 2nd line has executed, the range only
contains the page-break. The online help explicitly states
that "When you insert a page or column break, the range or
selection is replaced by the break. If you don't want to
replace the range or selection, use the Collapse method
before using the InsertBreak method." What is going wrong?
This is Office XP, by the way.

Dave
 
H

Helmut Weber

Hi Dave,
as far as I understand this, nothing is wrong.
You collapsed the range to the end. So it contains
nothing. You insert a pagebreak. The range contains
the pagebreak. Otherwise, if the help is right,
what I'm assuming here, your document would consist of
nothing as a pagebreak at this point.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT4.0
 
K

Klaus Linke

Hi Dave,

Try

With ActiveDocument.Range
.Characters.Last.InsertBreak Type:=wdPageBreak
.InsertAfter vbCrLf & vbCrLf & "This is 2nd page of text"
End With

ActiveDocument.Range is the range of all the content.

It doesn't make sense to use .Collapse on this:
ActiveDocument.Range will still be the same range (encompassing all the
content) afterwards.

..InsertBreak replaces the range you use it on with a break, so if you use
it on ActiveDocument.Range, you'll delete everything, as Helmut said.

If you use .InsertBreak on the last character in the document, you replace
only that last character (so no need to collapse).
In fact this isn't strictly true: The last character is always a paragraph
mark, and you can't replace or delete that.
So afterwards, there will still be a character at the very end of the
document.

Same with ActiveDocument.Range.InsertAfter: It can't really insert anything
after the final paragraph mark, so whatever you insert will end up before
the final paragraph mark (at the very end of the document).

Regards,
Klaus
 

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