Preventing a Table from spanning multiple pages

D

doctorjones_md

Is there a way that I can keep a Table (which is inserted into Document1
from another Word document) from spanning multiple pages? For example, with
the following code, if Table(8) in Document1 is near the bottom of the page,
I need to write some code to check to see if there is room on the page to
ADD a new Row and insert the data from ExportDoc without having to carry
over onto the next page -- basically, if there isn't room on the page to
include the NEW row of data, then I need to insert a page break before
Table(8) in Document1 -- how can I do this?

Option Explicit

Dim pTable1 As Table
Dim pTable2 As Table
Dim pIndex As Long
Dim pRange As Word.Range
Dim ExportDoc As Word.Document

Private Sub cbxDelivery_Click()

Set ExportDoc = Documents.Open("H:\Services\Delivery.doc")
Set pTable1 = ExportDoc.Tables(1)
Set pTable2 = Documents("Document1").Tables(8) 'Sets Table 8 as the
insertion point

If Me.cbxMS_IDE.Value = True Then

pTable1.Rows.Add BeforeRow:=pTable2.Rows(3) 'Sets the insertion
point before Row 3
For pIndex = 1 To pTable1.Columns.Count
Set pRange = pTable1.Cell(2, pIndex).Range 'Selects Row 2 in
ExportDoc
pRange.End = pRange.End - 1
pRange.Copy
pTable2.Cell(3, pIndex).Range.Paste 'Pastes data in Row 3 of
Table 8 in Document1

Next

Me.cmdOK.Enabled = True

End If

ExportDoc.Close
Set ExportDoc = Nothing

End Sub

Much Thanks in Advance

Shane
 
J

Jezebel

You can use the Information() function to tell you what page a range is on:
so you could check what page the table starts on --

pTable.Cell(1,1).Range.Information(wdActiveEndPageNumber)

then add your row and check what page the first cell of the added row is on.
If different, insert your page break (or set the style for the first cell to
'page break before').

Alternatively, you might be able to solve the problem by setting all the
styles in the table to 'Keep with next'. In general, style-based solutions
are better than code-based: less work, and the document is more likely to
remain correct after subsequent editing or if displayed on a different
computer.
 
D

doctorjones_md

Jezebel,

I took your advice to set the styles in the tables to "Keep With Next" --
this works brillantly -- thanks again!

Shane
=================
 
L

Lüko Willms

I took your advice to set the styles in the tables to "Keep With Next" --
this works brillantly -- thanks again!

I'm not sure if this is what I have in mind -- my MS-Word 'speaks'
German and uses different designations -- but you can

a) activate the option that a page break within a row is not
permitted;
e.g. like this: myTable.AllowPageBreaks = False

and
b) designate the first Row(s) as table headers which are to be
repeated on every page.
e.g. like this: myTable.Rows(1).HeadingFormat = True



Yours,
L.W.
 
D

Doctorjones_md

Luko,

Thanks for your suggestion -- a variation of what Jezebel suggested as
well -- this solved my problem.

Thanks again.
 

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