C# inserting PageBreaks

Z

Zumwalt

What I am trying to do, is accurately place page breaks after certain
conditions.
The following only works at placing the break last, no matter how many
paragraphs come after it in the code:

Code:
object oPageBreak = Word.WdBreakType.wdPageBreak;

wrdRng[cvg] = aDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng[cvg].InsertBreak(ref oPageBreak);

No matter where I place this in the code, page breaks are added only at the
end of the entire document. So say for instance, I have 10 paragraphs, then
that code, that "should" mean that the page break happens "at that point",
then any proceeding paragraphs should start at the next page after the break.

Again, I have another 10 paragraphs, then that line of code, well, the first
10 paragraphs and the second 10 paragraphs are together and there are 2 blank
pages at the end.

I then tried the following:

Code:
object oPageBreak = Word.WdBreakType.wdPageBreak;

oP[lineX] = aDoc.Content.Paragraphs.Add(ref missing);
oP[lineX].Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
oP[lineX].Range.Font.Bold = 1;
oP[lineX].Range.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
oP[lineX].Range.Text = "Prior Document Data";
oP[lineX].Range.InsertParagraphAfter();
oP[lineX].Range.InsertBreak(ref oPageBreak);

This had utterly no effect of adding a page break either.
Does anyone know the correct syntax to insert a page break at a given point
witihn a range?
 
C

Cindy M.

Hi =?Utf-8?B?WnVtd2FsdA==?=,
Code:
object oPageBreak = Word.WdBreakType.wdPageBreak;

wrdRng[cvg] = aDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng[cvg].InsertBreak(ref oPageBreak);

No matter where I place this in the code, page breaks are added only at the
end of the entire document.
What have you assigned to oEndOfDoc? According to the name of the variable, at
least, it seems logical the page breaks would be positioned at the end of the
document... The key is more likely how you're inserting the rest of the
document content.

I'm guessing you need to re-set the target range to be at the end of the
document, after the page break has been inserted. For some types of document
content (fields come to mind) Word doesn't extend the range to include them.
You inserting the page break at the end of the document, rather than at the
end of the range, may be the problem - the end of the document is probably
calculated as being after the range.

Personally, I like to work with the Collapse method, something like this:

Dim rng as Word.Range
Set rng = oDoc.content
rng.Text = "something here"
rng.Collapse wdCollapseEnd 'Word.WdCollapseDirection.wdCollapseEnd
'now anything I insert will follow the above
rng.InsertBreak oPageBreak
rng.Collapse wdCollapseEnd
rng.Text = "more text"

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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