How to get a Word document to go to a new page using VB.NET

L

lou_1

hey all,

ok, what i am trying to do is, by using the Word Automation Porgramming
Objects, to dynamically create a Word document using VB.NET. Here is my code
so far:

Dim objWord As Word.Application
Dim objDoc0 As Word.Document, objDoc1 As Word.Document
Dim objTable As Word.Table
Dim objShape As Word.InlineShape

'Start Word and open the document template.
objWord = CreateObject("Word.Application")
objWord.Visible = True
objDoc0 = objWord.Documents.Add

objWord.MailingLabel.DefaultPrintBarCode = False
objDoc1 = objWord.MailingLabel.CreateNewDocument(Name:="5162")

objDoc1.Tables.Item(1).Range.ParagraphFormat.SpaceAfter = 0
objDoc1.Tables.Item(1).AllowPageBreaks = True

For rowCounter = 1 To objDoc1.Tables.Item(1).Rows.Count
For colCounter = 1 To objDoc1.Tables.Item(1).Columns.Count
If (objDoc1.Tables.Item(1).Cell(rowCounter,
colCounter).Width >= objWord.InchesToPoints(MIN_LABEL_WIDTH) And
objDoc1.Tables.Item(1).Cell(rowCounter, colCounter).Height >=
objWord.InchesToPoints(MIN_LABEL_HEIGHT)) Then
objDoc1.Tables.Item(1).Cell(rowCounter,
colCounter).Range.InsertParagraphBefore()

'Code to deremine the value of txtLabel.
'Basically i have a loop here that loops through a bunch
of stuff and
'genearates a number of values for txtLabel.


objDoc1.Tables.Item(1).Cell(rowCounter,
colCounter).Range.Text = txtLabel


objDoc1.Tables.Item(1).Cell(rowCounter,
colCounter).Range.Cells.VerticalAlignment =
word.WdCellVerticalAlignment.wdCellAlignVerticalCenter
objDoc1.Tables.Item(1).Cell(rowCounter,
colCounter).Range.ParagraphFormat.Alignment =
word.WdParagraphAlignment.wdAlignParagraphCenter
objDoc1.Tables.Item(1).Cell(rowCounter,
colCounter).Range.Rows.Alignment = word.WdRowAlignment.wdAlignRowCenter

End If
Next colCounter
Next rowCounter

So basically it works, but the problem is that it only generates one page,
which is fine if the data i want to display is less than a page long.
However, if i have a lot of data to put on the document i need for it to open
as many pages as needed and right now it is not doing that.

Help please!
Thanks!
 
D

Doug Robbins

Starting with the selection in the first label (cell), if each time you
insert txtlabel, you issue a

Selection.MoveRight Unit:=wdCell

the table will expand to accommodate all of the data.

It may be more efficient however to use some code to determine the number of
labels to be created and create (add) the necessary number of rows to the
table and then insert the data into the range of each cell.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
Top