Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Insert Page Breaks

Status
Not open for further replies.

BlindPete

Programmer
Jul 5, 2000
711
1
0
US
I am having a devil of a time inserting page breaks with VBA. I am combing documents by inserting them into a single document. I want to insert a page break between documents. I have tried a variety of methods and none work. I typically end up with a single break at the end of the document.

Code:
Do While Not oDB.dbRecordSet.EOF
  Set oRange = oDoc.Content.Bookmarks.Item("\endofdoc").Range
  oRange.InsertFile App.Path + "\qwerty.html", "", False, False, False

  'Page Break
  Set oRange = oDoc.Content.Bookmarks.Item("\endofdoc").Range
  Set oParagraph = oDoc.Paragraphs.Add(oRange)
  oParagraph.Range.InsertBreak wdPageBreak '<-- DOES NOT WORK
  oDB.dbRecordSet.MoveNext
Loop

-Pete
Games the old fashion way with Dice, Paper and Pencils!
 
Do not use:
Code:
oDoc.Content.Bookmarks.Item("\endofdoc").Range
Why are you doing that? It is a very strange usage.

Also what are you trying to do with:
Code:
Set oParagraph = oDoc.Paragraphs.Add(oRange)

try something like:
Code:
Dim oRange As Range
Set oRange = ActiveDocument.Range

Do While Not oDB.dbRecordSet.EOF
   With oRange
      .Collapse Direction:=wdCollapseEnd
      .InsertFile App.Path + "\qwerty.html", "", _
              False, False, False
   End With
   Set oRange = ActiveDocument.Range
   With oRange
      .Collapse Direction:=wdCollapseEnd
      .InsertBreak Type:=wdPageBreak
   End With
  oDB.dbRecordSet.MoveNext
Loop
Each iteration of the loop collapses the Range object to the end (in this case the document end), inserts the file, RESETS the Range object to the document again, collapses to end again (now at the end of the newly inserted file), inserts a page break, and continues.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks Gerry,

I am trying to insert a page break at the end of the document without using selection object. I had read in several places that selection object was a poor choice and to avoid using them. I am a complete novice with Word's COM model. Excel's... I can write code in my sleep!

So the Bookmark "\endofdoc" I found on MSDN
'Insert a paragraph at the end of the document.
'** \endofdoc is a predefined bookmark.
oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
I was using this as a means to get to the end of the document.

I do not understand the context you posted wrt to .Collapse method. What does that mean? Go to the end of the range?

I replaced my \endofdoc methodology with the one you described. All else still works but I still don't get the page breaks. Drrratttt!

Code:
Do While Not oDB.dbRecordSet.EOF
  Set oRange = ActiveDocument.Range
  oRange.Collapse Direction:=wdCollapseEnd
  Set oParagraph = oDoc.Paragraphs.Add(oRange)
  oParagraph.Range.Text = oDB.dbRecordSet.Fields("title").Value
  oParagraph.Range.Style = "Heading 1"
  oParagraph.Range.InsertParagraphAfter

  'Generate HTML doc from DB 
  '... deleted ...
   
  Set oRange = ActiveDocument.Range
  oRange.Collapse Direction:=wdCollapseEnd
  oRange.InsertFile App.Path + "\qwerty.html", "", False, False, False

  'Page Break
  Set oRange = ActiveDocument.Range
  oRange.Collapse Direction:=wdCollapseEnd
  oRange.InsertBreak Type:=wdPageBreak

  oDB.dbRecordSet.MoveNext
Loop

-Pete
Games the old fashion way with Dice, Paper and Pencils!
 
well bizarre though it may be this code works

Code:
'Page Break
Set oRange = ActiveDocument.Range
oRange.Collapse Direction:=wdCollapseEnd
Set oParagraph = oDoc.Paragraphs.Add(oRange)
oParagraph.Range.InsertParagraphAfter
oParagraph.Range.InsertBreak wdPageBreak

Only works for "InsertParagraphAfter" and only if this line is included. But it does work.

-Pete
Games the old fashion way with Dice, Paper and Pencils!
 
I am trying to insert a page break at the end of the document without using selection object. I had read in several places that selection object was a poor choice and to avoid using them. "

Indeed, that is correct. It is a good idea to avoid using Selection.

My code did not use Selection at all, it use Range.

Collapse? It means precisely that. A Range has a Start and and End. Collapse, well, collapses the Range so its Start and End values are equal, ie. it is a single character.

You can use Direction:=wdCollapseEnd, or wdCollapseStart. In the first case, it collapse to the End Value, in the latter it collapses to the Start Value.

I still do not understand your use of oParagraph. Although I can sort of see it, as you are adding a text string (with a style) you did not mention before.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top