How to prevent table break , if the table size is below the word doc size.
ie if the table size is below word doc size dont spilt the table across
table, it has to shift to new page. if the table size is above doc it can
spilt. this is the code i used for creating table and writing data to it

here how many tables are there and where the table starts in docand how many
rows the there is known only after table is created..

Set rs2 = New ADODB.Recordset
oConnection_SQLserver.Open
rs2.Open "SELECT logTime, logType, Comment1,Comment2,logStatus FROM
TestEvent WHERE TestScriptID = " & lngTestScriptID & " AND TestCaseTime = " &
rs1(2).Value & " AND logType <> 5 ORDER BY logTime", oConnection_SQLserver,
adOpenStatic, adLockReadOnly

If (rs2.EOF = True) Then
GoTo ENDOFLOOP
End If

'Add a table to print the testevents.
rng.Tables.Add Range:=rng, NumRows:=2, NumColumns:=3

'enter the first row in the table
Set tbl = rng.Tables(1)
tbl.Cell(1, 1).Range.Text = "Action"
tbl.Cell(1, 2).Range.Text = "Results"
tbl.Cell(1, 3).Range.Text = "Test Result"

'expected Results flag is set to 1 on receiving a testevent with
logstatus equal to 1 or 3
expectedResultsFlag = 0
rowCount = 2
flagTestEventFail = False
'repeat for all testevents in each selected testcase
Do While (rs2.EOF = False)

'move to next row when expectedResultsFlag = 1
If (expectedResultsFlag = 1 And (rs2.Fields(1) = 0 Or
rs2.Fields(1) = 2)) Then
tbl.Rows.Add
rowCount = rowCount + 1
expectedResultsFlag = 0
flagTestEventFail = False
End If

'change the color to RED only if the testevent is Fail else
leave it as Black, which is default
If (rs2.Fields(4) = 0) Then
'case Fail
tbl.Cell(rowCount, 1).Range.Font.Color = wdColorRed
tbl.Cell(rowCount, 2).Range.Font.Color = wdColorRed
flagTestEventFail = True

Else
If (rs2.Fields(4) = 1 And flagTestEventFail = False) Then
'case Pass
tbl.Cell(rowCount, 1).Range.Font.Color = wdColorBlack
tbl.Cell(rowCount, 2).Range.Font.Color = wdColorBlack
flagTestEventFail = True
End If
End If

'Check the logstatus field
If (rs2.Fields(1) = 0 Or rs2.Fields(1) = 2) Then
'enter the testevent under the "Actions" column in the table
tbl.Cell(rowCount, 1).Range.InsertAfter (rs2.Fields(2) & " "
& rs2.Fields(3))
tbl.Cell(rowCount, 1).Range.InsertParagraphAfter
tbl.Cell(rowCount, 1).Range.SetRange tbl.Cell(rowCount,
1).Range.End, tbl.Cell(rowCount, 1).Range.End
ElseIf (rs2.Fields(1) = 1 Or rs2.Fields(1) = 3) Then
'enter the testevent under the "Expected Results" column in
the table
tbl.Cell(rowCount, 2).Range.InsertAfter (rs2.Fields(2) & " "
& rs2.Fields(3))
tbl.Cell(rowCount, 2).Range.InsertParagraphAfter
tbl.Cell(rowCount, 2).Range.SetRange tbl.Cell(rowCount,
2).Range.End, tbl.Cell(rowCount, 2).Range.End
expectedResultsFlag = 1
End If

rs2.MoveNext

RE: preventing table break across table by JeanGuyMarcil

JeanGuyMarcil
Tue May 20 05:01:00 PDT 2008

"jishith" wrote:

>
> How to prevent table break , if the table size is below the word doc size.
> ie if the table size is below word doc size dont spilt the table across
> table, it has to shift to new page. if the table size is above doc it can
> spilt. this is the code i used for creating table and writing data to it
>
> here how many tables are there and where the table starts in docand how many
> rows the there is known only after table is created..

I am not sure I understand. I believe you want to keep the table on the same
page, i.e., not break across two pages.

Since you are using a table object in your code, you can easily adapt the
followiing:


Dim rng As Range
Dim tbl As Table

Set rng = ActiveDocument.Range
Set tbl = rng.Tables(1)

tbl.Range.ParagraphFormat.KeepWithNext = True


It will force the table on the same page; but, if the table is too long to
fit on one page, it will nevertheless start on the next page, but overflow
onto another page as needed.


RE: preventing table break across table by jishith

jishith
Wed May 21 01:14:01 PDT 2008

Hello Jean,

thanks for the posting.
If the table starts almost at end of the page and want to continue to next
page the entire table has to move to next page(if its size is less than page
size). Else the table can spilt across the page.
Hope u got my problem. if i used ur code the table won't spilt , it just
over flow .. right.
If i get the page size and the table size can check a condtion and prevent
break.
Any other idea for the above issue

"Jean-Guy Marcil" wrote:

> "jishith" wrote:
>
> >
> > How to prevent table break , if the table size is below the word doc size.
> > ie if the table size is below word doc size dont spilt the table across
> > table, it has to shift to new page. if the table size is above doc it can
> > spilt. this is the code i used for creating table and writing data to it
> >
> > here how many tables are there and where the table starts in docand how many
> > rows the there is known only after table is created..
>
> I am not sure I understand. I believe you want to keep the table on the same
> page, i.e., not break across two pages.
>
> Since you are using a table object in your code, you can easily adapt the
> followiing:
>
>
> Dim rng As Range
> Dim tbl As Table
>
> Set rng = ActiveDocument.Range
> Set tbl = rng.Tables(1)
>
> tbl.Range.ParagraphFormat.KeepWithNext = True
>
>
> It will force the table on the same page; but, if the table is too long to
> fit on one page, it will nevertheless start on the next page, but overflow
> onto another page as needed.
>

Re: preventing table break across table by Doug

Doug
Wed May 21 02:16:11 PDT 2008

Format all the paragraphs in the table so that the lines are kept together
and kept with next. That will cause the table to flop over to the next page
if it won't all fit on the current page, but the settings will be ignored if
the table is too big to fit on one page.

--
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

"jishith" <jishith@discussions.microsoft.com> wrote in message
news:B797B022-6814-44ED-94A9-E3B550CBA3F7@microsoft.com...
> Hello Jean,
>
> thanks for the posting.
> If the table starts almost at end of the page and want to continue to next
> page the entire table has to move to next page(if its size is less than
> page
> size). Else the table can spilt across the page.
> Hope u got my problem. if i used ur code the table won't spilt , it just
> over flow .. right.
> If i get the page size and the table size can check a condtion and prevent
> break.
> Any other idea for the above issue
>
> "Jean-Guy Marcil" wrote:
>
>> "jishith" wrote:
>>
>> >
>> > How to prevent table break , if the table size is below the word doc
>> > size.
>> > ie if the table size is below word doc size dont spilt the table across
>> > table, it has to shift to new page. if the table size is above doc it
>> > can
>> > spilt. this is the code i used for creating table and writing data to
>> > it
>> >
>> > here how many tables are there and where the table starts in docand how
>> > many
>> > rows the there is known only after table is created..
>>
>> I am not sure I understand. I believe you want to keep the table on the
>> same
>> page, i.e., not break across two pages.
>>
>> Since you are using a table object in your code, you can easily adapt the
>> followiing:
>>
>>
>> Dim rng As Range
>> Dim tbl As Table
>>
>> Set rng = ActiveDocument.Range
>> Set tbl = rng.Tables(1)
>>
>> tbl.Range.ParagraphFormat.KeepWithNext = True
>>
>>
>> It will force the table on the same page; but, if the table is too long
>> to
>> fit on one page, it will nevertheless start on the next page, but
>> overflow
>> onto another page as needed.
>>



Re: preventing table break across table by jishith

jishith
Wed May 21 03:14:01 PDT 2008

Hello Robbin,

Can u give the code for that. i having no experience in Vba.

"Doug Robbins - Word MVP" wrote:

> Format all the paragraphs in the table so that the lines are kept together
> and kept with next. That will cause the table to flop over to the next page
> if it won't all fit on the current page, but the settings will be ignored if
> the table is too big to fit on one page.
>
> --
> 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
>
> "jishith" <jishith@discussions.microsoft.com> wrote in message
> news:B797B022-6814-44ED-94A9-E3B550CBA3F7@microsoft.com...
> > Hello Jean,
> >
> > thanks for the posting.
> > If the table starts almost at end of the page and want to continue to next
> > page the entire table has to move to next page(if its size is less than
> > page
> > size). Else the table can spilt across the page.
> > Hope u got my problem. if i used ur code the table won't spilt , it just
> > over flow .. right.
> > If i get the page size and the table size can check a condtion and prevent
> > break.
> > Any other idea for the above issue
> >
> > "Jean-Guy Marcil" wrote:
> >
> >> "jishith" wrote:
> >>
> >> >
> >> > How to prevent table break , if the table size is below the word doc
> >> > size.
> >> > ie if the table size is below word doc size dont spilt the table across
> >> > table, it has to shift to new page. if the table size is above doc it
> >> > can
> >> > spilt. this is the code i used for creating table and writing data to
> >> > it
> >> >
> >> > here how many tables are there and where the table starts in docand how
> >> > many
> >> > rows the there is known only after table is created..
> >>
> >> I am not sure I understand. I believe you want to keep the table on the
> >> same
> >> page, i.e., not break across two pages.
> >>
> >> Since you are using a table object in your code, you can easily adapt the
> >> followiing:
> >>
> >>
> >> Dim rng As Range
> >> Dim tbl As Table
> >>
> >> Set rng = ActiveDocument.Range
> >> Set tbl = rng.Tables(1)
> >>
> >> tbl.Range.ParagraphFormat.KeepWithNext = True
> >>
> >>
> >> It will force the table on the same page; but, if the table is too long
> >> to
> >> fit on one page, it will nevertheless start on the next page, but
> >> overflow
> >> onto another page as needed.
> >>
>
>
>

Re: preventing table break across table by Doug

Doug
Wed May 21 05:01:56 PDT 2008

This will do it for the table in which the selection is located:

With Selection.Tables(1).Range.ParagraphFormat
.KeepTogether = True
.KeepWithNext = True
End With


--
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

"jishith" <jishith@discussions.microsoft.com> wrote in message
news:E39BA2A7-B783-4C0D-BDF4-4B5D90BEFDEB@microsoft.com...
> Hello Robbin,
>
> Can u give the code for that. i having no experience in Vba.
>
> "Doug Robbins - Word MVP" wrote:
>
>> Format all the paragraphs in the table so that the lines are kept
>> together
>> and kept with next. That will cause the table to flop over to the next
>> page
>> if it won't all fit on the current page, but the settings will be ignored
>> if
>> the table is too big to fit on one page.
>>
>> --
>> 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
>>
>> "jishith" <jishith@discussions.microsoft.com> wrote in message
>> news:B797B022-6814-44ED-94A9-E3B550CBA3F7@microsoft.com...
>> > Hello Jean,
>> >
>> > thanks for the posting.
>> > If the table starts almost at end of the page and want to continue to
>> > next
>> > page the entire table has to move to next page(if its size is less
>> > than
>> > page
>> > size). Else the table can spilt across the page.
>> > Hope u got my problem. if i used ur code the table won't spilt , it
>> > just
>> > over flow .. right.
>> > If i get the page size and the table size can check a condtion and
>> > prevent
>> > break.
>> > Any other idea for the above issue
>> >
>> > "Jean-Guy Marcil" wrote:
>> >
>> >> "jishith" wrote:
>> >>
>> >> >
>> >> > How to prevent table break , if the table size is below the word doc
>> >> > size.
>> >> > ie if the table size is below word doc size dont spilt the table
>> >> > across
>> >> > table, it has to shift to new page. if the table size is above doc
>> >> > it
>> >> > can
>> >> > spilt. this is the code i used for creating table and writing data
>> >> > to
>> >> > it
>> >> >
>> >> > here how many tables are there and where the table starts in docand
>> >> > how
>> >> > many
>> >> > rows the there is known only after table is created..
>> >>
>> >> I am not sure I understand. I believe you want to keep the table on
>> >> the
>> >> same
>> >> page, i.e., not break across two pages.
>> >>
>> >> Since you are using a table object in your code, you can easily adapt
>> >> the
>> >> followiing:
>> >>
>> >>
>> >> Dim rng As Range
>> >> Dim tbl As Table
>> >>
>> >> Set rng = ActiveDocument.Range
>> >> Set tbl = rng.Tables(1)
>> >>
>> >> tbl.Range.ParagraphFormat.KeepWithNext = True
>> >>
>> >>
>> >> It will force the table on the same page; but, if the table is too
>> >> long
>> >> to
>> >> fit on one page, it will nevertheless start on the next page, but
>> >> overflow
>> >> onto another page as needed.
>> >>
>>
>>
>>



RE: preventing table break across table by JeanGuyMarcil

JeanGuyMarcil
Wed May 21 06:12:01 PDT 2008

"jishith" wrote:

> Hello Jean,
>
> thanks for the posting.
> If the table starts almost at end of the page and want to continue to next
> page the entire table has to move to next page(if its size is less than page
> size). Else the table can spilt across the page.
> Hope u got my problem. if i used ur code the table won't spilt , it just
> over flow .. right.
> If i get the page size and the table size can check a condtion and prevent
> break.
> Any other idea for the above issue

Use my suggestion (tbl.Range.ParagraphFormat.KeepWithNext = True) for all
tables you create.
After you have applied the "KeepWithNext" code, get the page number for the
first row of the table. Compare that page number with the page number for the
last row. If they are different, you know the table spans more than one page.
In that case, remove the Keep With Next setting to let the table flow
normally from the bottom of a page to the next page.

You could use code like this:

Dim tbl As Table
Dim lngPageStart As Long
Dim lngPageEnd As Long

Set tbl = ActiveDocument.Tables(1)

With tbl
.Range.ParagraphFormat.KeepWithNext = True
lngPageStart = .Rows(1).Cells(1).Range _
.Information(wdActiveEndPageNumber)
lngPageEnd = .Rows(.Rows.Count).Cells(1) _
.Range.Information(wdActiveEndPageNumber)
If lngPageEnd > lngPageStart Then
.Range.ParagraphFormat.KeepWithNext = False
End If
End With

RE: preventing table break across table by jishith

jishith
Wed May 21 23:10:02 PDT 2008

Hello jean,

That code is good. But these may cause an issue if the table is greater than
page size. What happens to a table if it greater than page size? So i also
want to check the table size before spilting. How to get table size. and i
want that table heading to be shifted to next page. Give code considering
that one too.

u have any suggestion do so.



"Jean-Guy Marcil" wrote:

> "jishith" wrote:
>
> > Hello Jean,
> >
> > thanks for the posting.
> > If the table starts almost at end of the page and want to continue to next
> > page the entire table has to move to next page(if its size is less than page
> > size). Else the table can spilt across the page.
> > Hope u got my problem. if i used ur code the table won't spilt , it just
> > over flow .. right.
> > If i get the page size and the table size can check a condtion and prevent
> > break.
> > Any other idea for the above issue
>
> Use my suggestion (tbl.Range.ParagraphFormat.KeepWithNext = True) for all
> tables you create.
> After you have applied the "KeepWithNext" code, get the page number for the
> first row of the table. Compare that page number with the page number for the
> last row. If they are different, you know the table spans more than one page.
> In that case, remove the Keep With Next setting to let the table flow
> normally from the bottom of a page to the next page.
>
> You could use code like this:
>
> Dim tbl As Table
> Dim lngPageStart As Long
> Dim lngPageEnd As Long
>
> Set tbl = ActiveDocument.Tables(1)
>
> With tbl
> .Range.ParagraphFormat.KeepWithNext = True
> lngPageStart = .Rows(1).Cells(1).Range _
> .Information(wdActiveEndPageNumber)
> lngPageEnd = .Rows(.Rows.Count).Cells(1) _
> .Range.Information(wdActiveEndPageNumber)
> If lngPageEnd > lngPageStart Then
> .Range.ParagraphFormat.KeepWithNext = False
> End If
> End With

RE: preventing table break across table by JeanGuyMarcil

JeanGuyMarcil
Thu May 22 05:00:01 PDT 2008

"jishith" wrote:

> Hello jean,
>
> That code is good. But these may cause an issue if the table is greater than
> page size. What happens to a table if it greater than page size? So i also
> want to check the table size before spilting. How to get table size. and i
> want that table heading to be shifted to next page. Give code considering
> that one too.

I will suggest that you read my post again, but with more attention and
trying to actually unerstand what I wrote... ;-)

I specifically addressed the issue of the the table being longer than one
page. The sentence "If they are different, you know the table spans more than
one page" should give you a clue...
Also, I never wrote anything about splitting a table...

Finally, for the header to repeat across pages, apply the "HeadingFormat"
property to the last rows you want as header:
tbl.Rows(3).HeadingFormat = True
This code will make the first three rows of the "tbl" table repeat as headers.

RE: preventing table break across table by Jishith

Jishith
Thu May 22 10:30:01 PDT 2008

Sorry jean,

If in a page three tables are there( twoo small tables(t1 and t2) and one
table more than half the size of page(t3)). t3 starts mid of the page. due to
its size it continue to next page. But t3 size is less than page size. if we
check the page no. it will be different. but what i actually want is that
table has to move to next page there it can fit confortly.

"Jean-Guy Marcil" wrote:

> "jishith" wrote:
>
> > Hello jean,
> >
> > That code is good. But these may cause an issue if the table is greater than
> > page size. What happens to a table if it greater than page size? So i also
> > want to check the table size before spilting. How to get table size. and i
> > want that table heading to be shifted to next page. Give code considering
> > that one too.
>
> I will suggest that you read my post again, but with more attention and
> trying to actually unerstand what I wrote... ;-)
>
> I specifically addressed the issue of the the table being longer than one
> page. The sentence "If they are different, you know the table spans more than
> one page" should give you a clue...
> Also, I never wrote anything about splitting a table...
>
> Finally, for the header to repeat across pages, apply the "HeadingFormat"
> property to the last rows you want as header:
> tbl.Rows(3).HeadingFormat = True
> This code will make the first three rows of the "tbl" table repeat as headers.

RE: preventing table break across table by JeanGuyMarcil

JeanGuyMarcil
Thu May 22 11:11:01 PDT 2008

"Jishith" wrote:

> Sorry jean,
>
> If in a page three tables are there( twoo small tables(t1 and t2) and one
> table more than half the size of page(t3)). t3 starts mid of the page. due to
> its size it continue to next page. But t3 size is less than page size. if we
> check the page no. it will be different. but what i actually want is that
> table has to move to next page there it can fit confortly.
>

This is the second time I tell you that I have already answered this concern
you are having (which I understood as soon as you brought it up...)
Are you actually reading the suggestions we are offering?
Two posts ago, I wrote, and I quote:

"Use my suggestion (tbl.Range.ParagraphFormat.KeepWithNext = True) for all
tables you create.
After you have applied the "KeepWithNext" code, get the page number for the
first row of the table. Compare that page number with the page number for the
last row. If they are different, you know the table spans more than one page.
In that case, remove the Keep With Next setting to let the table flow
normally from the bottom of a page to the next page."

And then I go on the explain what to do.

What more do you want? Are you having specific problems with the
implementation of these ideas?

RE: preventing table break across table by jishith

jishith
Thu May 22 23:44:02 PDT 2008

Hello Jean,
Thanks for your suggestions, i used the same code u gave and its working
fine. one small problem is that if the table size is greater than page size.
that table also shifting to entirely to new page. that one is not needed. any
way it has to spilt once.if u stilll didnt get my problem leave it. Thanks
alot for ur sugestion.



"Jean-Guy Marcil" wrote:

> "Jishith" wrote:
>
> > Sorry jean,
> >
> > If in a page three tables are there( twoo small tables(t1 and t2) and one
> > table more than half the size of page(t3)). t3 starts mid of the page. due to
> > its size it continue to next page. But t3 size is less than page size. if we
> > check the page no. it will be different. but what i actually want is that
> > table has to move to next page there it can fit confortly.
> >
>
> This is the second time I tell you that I have already answered this concern
> you are having (which I understood as soon as you brought it up...)
> Are you actually reading the suggestions we are offering?
> Two posts ago, I wrote, and I quote:
>
> "Use my suggestion (tbl.Range.ParagraphFormat.KeepWithNext = True) for all
> tables you create.
> After you have applied the "KeepWithNext" code, get the page number for the
> first row of the table. Compare that page number with the page number for the
> last row. If they are different, you know the table spans more than one page.
> In that case, remove the Keep With Next setting to let the table flow
> normally from the bottom of a page to the next page."
>
> And then I go on the explain what to do.
>
> What more do you want? Are you having specific problems with the
> implementation of these ideas?

RE: preventing table break across table by JeanGuyMarcil

JeanGuyMarcil
Fri May 23 04:53:01 PDT 2008

"jishith" wrote:

> Hello Jean,
> Thanks for your suggestions, i used the same code u gave and its working
> fine. one small problem is that if the table size is greater than page size.
> that table also shifting to entirely to new page. that one is not needed. any
> way it has to spilt once.if u stilll didnt get my problem leave it. Thanks
> alot for ur sugestion.
>

This the third, and last time, I address this issue, unless you come up with
a different question!
In my previous post I refered you to my other previous post where I did
answer this question you have asked three or four times, even tough I have
already answered.

The code I posted then was:


Dim tbl As Table
Dim lngPageStart As Long
Dim lngPageEnd As Long

Set tbl = ActiveDocument.Tables(1)

With tbl
.Range.ParagraphFormat.KeepWithNext = True
lngPageStart = .Rows(1).Cells(1).Range _
.Information(wdActiveEndPageNumber)
lngPageEnd = .Rows(.Rows.Count).Cells(1) _
.Range.Information(wdActiveEndPageNumber)
If lngPageEnd > lngPageStart Then
.Range.ParagraphFormat.KeepWithNext = False
End If
End With


I have just tested it again (Word 2003), and it works as I claim. If the
table is longer than one page, it will not move to the begining of the next
page. It will stay put at the bottom of the page where it was initially
inserted/created.

If you get different results, then you are either not applying the code
correctly or there are other factors at play that you may not be aware of.
Try the code with a simple document containing a few paragraphs and a table
at the bottom of the page, this table should be shorter than one page. Then,
undo the changes the code does, make the table longer by adding enough rows
so that the table is now longer than a page. Run the code again, you will see
that the table stays where it is, it does not move to the top of the next
page.