PDA

View Full Version : Table Breaking Across Pages Problem



dfenton21
09-15-2012, 06:16 AM
I'm a VBA developer for about 3 years, and I've just started work on my first Word project. I much prefer the Excel object model, but that it is a different story.

Basically, I need to create a new document and place text at very specific locations. For numerous reasons, each block of text must be within a one cell table. Normally, when I am just manually typing a document and I need specific control of a tables's y location, I just set the font size of the previous paragragh to 1 and increase pixel by pixel as required.

I don't want to use such a messy approach for an automated document, so I've used the folllowing code to set a table's specific location:


Sub foo()
Dim tbl As Table
Set tbl = ActiveDocument.Tables.Add(ActiveDocument.Range, 1, 1)

tbl.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
tbl.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
tbl.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
tbl.Borders(wdBorderRight).LineStyle = wdLineStyleSingle

tbl.Rows.VerticalPosition = 600
tbl.Rows(1).AllowBreakAcrossPages = False

tbl.Cell(1, 1).Range.Text = "Text Text Text Text Text"

End Sub



Note that I've not allowed the row to break across pages, which is important for my requirements.

My problem is as follows:
If I manually insert a table, and do not allow it to break across pages, the entire table will move to the next page if the text takes up too much space to fit on the first page.(which is correct).
But when I insert the table using the above code, if the text takes up too much space, the table's height just increases instead of moving to the next page.

I believe this behaviour is related to the fact that by using the code to insert the table, it will not insert paragraph marks throughout the page.

Is there a way around this problem? Basically my VBA requirements are:

Insert a one cell table at a specific location on a document
Table will move to the next page if its text is too big to fit in the remaining space on the page
The final document will end up containing dozens of these one cell tables.

Frosty
09-15-2012, 03:36 PM
You may need to look at the autofit property of the table as well. I think you've identified the problem, you just don't know all of the properties you're looking for. You don't want columns or heights of cells/columns/rows to adjust either. So try looking at setting those specifications as well.

dfenton21
09-23-2012, 12:41 PM
Thanks for your reply Frosty and sorry for the late reply.

I tried that and it didn't work. Anyway, I do need the row height to adjust - the tables will be populated via code and the text will be of differing sizes.

Any other suggestions?

macropod
09-23-2012, 03:58 PM
Cross-posted at: http://www.excelforum.com/word-programming-vba-macros/862667-word-table-pagination-problem.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

Frosty
09-24-2012, 09:29 AM
dfenton,

Sorry, I mis-understood-- I thought you were trying to have a single cell table be the entire height of the page, for some kind of formatting issue.

I think you should read the crossposting etiquette posted by Paul and in my signature, and your next post should include any other links to forums you have cross-posted this same question to.

From there, you'll need to post a sample document showing what you get when you insert a table manually, as well as when you create one via code.

I looked at Paul's link, but as I'm not a member there, I didn't see what code he actually posted.

You might be able to simply set this up as an autotext/buildingblock entry, if you are unable to find the code which formats the table exactly the way you want it formatted (an unlikely scenario, but possible, depending on the version of Word you are using).

I'm assuming you haven't recorded a macro when you create the table that works perfectlly in the manual fashion. You're probably just missing some of the properties you actually want.

fumei
09-24-2012, 11:31 AM
Jason, here is what Paul posted:

"Try something along the following lines. You may need to adjust the 597.6, which is why there's a message box. If you remove the comment mark before '& Strtxt', the table should move to the next page."

Sub foo()
Dim tbl As Table, Strtxt As String
Strtxt = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. Aenean nec lorem. In porttitor. Donec laoreet nonummy augue. Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla. Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, purus ipsum pretium metus, in lacinia nulla nisl eget sapien."
Set tbl = ActiveDocument.Tables.Add(ActiveDocument.Range.Characters.Last, 1, 1)
With tbl
.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
.Rows(1).AllowBreakAcrossPages = False
.Rows.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Rows.VerticalPosition = 600
.Rows(1).Range.ParagraphFormat.SpaceBefore = 0
.Cell(1, 1).Range.Text = Strtxt '& Strtxt
MsgBox .Rows(1).Range.Information(wdVerticalPositionRelativeToPage)
If .Rows(1).Range.Information(wdVerticalPositionRelativeToPage) < 597.6 Then
.Rows.WrapAroundText = False
End If
End With
End Sub

Note: I did not fix the very very very very very string with underscores...

dfenton21
10-02-2012, 11:11 AM
Sorry for the late reply - it's been a hectic few weeks.

Firstly, apologies for breaking etiquette. I always appreciate the advice I receive in forums and would never wish to annoy the regular posters.

Secondly, I tried Paul's suggestion but unfortuately it didn't work. As suggested, I've attached two docs:

"manual.docx" is where I manually inserted the one cell table and disabled the row breaking across pages. If you continue to type in that cell, there will be no more space on the first page, so the entire table moves to a second page.

"auto.docx" is where I inserted the table via the VBA I posted here. In this case, if you continue to type text, the table's height breaches the bottom margin of the page. When the very bottom of the page is reached, the table's height increases back up the page. I've included a screenshot of the result.

Basically, I want to achieve the behaviour of the manually inserted table with a table inserted via VBA.

I hope this is clear but please let me know if you require clarification.

dfenton21
10-02-2012, 11:11 AM
Here is manual.docx

macropod
10-02-2012, 03:43 PM
There is a fundamental difference between the tables in the two documents.

In auto.docx, the table is wrapped and, so, does not depend on the preceding content for the determination of its location. When there's too much to fit above the page margin, the table continues expanding down to the page border, then upwards. The '(don't) allow row to break across pages' attribute never forces it onto the next page. To force the table onto the next page requires a completely different approach, based on knowing how much content it has. Try the macro below.

The page the table is inserted on is controlled by:
Set Rng = ActiveDocument.GoTo(What:=wdGoToPage, Name:="1")
Change the '1' to suit.

The initial vertical position is controlled by:
.Rows.VerticalPosition = 600
Change the '600' to suit.

The allowable text height is controlled by:
If (sLast - sFirst) > 123.75 Then
Change the '123.75' to suit.

Sub InsertMyTable()
Dim Tbl As Table, Rng As Range, StrTxt As String, sFirst As Single, sLast As Single
StrTxt = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor " & _
"congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada " & _
"libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. " & _
"Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada " & _
"fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. Aenean nec lorem. " & _
"In porttitor. Donec laoreet nonummy augue. Suspendisse dui purus, scelerisque at, " & _
"vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut " & _
"nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. " & _
"Integer nulla. Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, " & _
"purus ipsum pretium metus, in lacinia nulla nisl eget sapien."
Set Rng = ActiveDocument.GoTo(What:=wdGoToPage, Name:="1")
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
With Rng
.Collapse wdCollapseEnd
If .Information(wdWithInTable) = True Then .Move wdCharacter, -1
End With
Set Tbl = ActiveDocument.Tables.Add(Rng, 1, 1)
With Tbl
.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
.Rows(1).AllowBreakAcrossPages = False
.Rows.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Rows.VerticalPosition = 600
.Rows(1).Range.ParagraphFormat.SpaceBefore = 0
With .Cell(1, 1).Range
.Text = StrTxt '& StrTxt
sFirst = .Characters.First.Information(wdVerticalPositionRelativeToPage)
sLast = .Characters.Last.Previous.Information(wdVerticalPositionRelativeToPage)
End With
If (sLast - sFirst) > 123.75 Then
With Rng
.Start = .Start - 1
.InsertBefore vbCr & Chr(12)
End With
.Rows.WrapAroundText = False
End If
End With
End Sub
Note also that you should not try to pad out the space before the table with empty paragraphs, etc. Doing so only unnecessarily complicates matters. If you have text that should start on the next page, use a manual page break after the last substantive content on the target page.

dfenton21
10-05-2012, 01:52 PM
Thanks for your help. Unfortuately, I'm, not getting the desired results. If the code within the IF statement at the bottom executes, the table just moves to the top of the current page, otherwise I get the same behaviour as before.

I'd appreciate any other suggestions?

Thanks again.

macropod
10-05-2012, 02:55 PM
When I tested the code - with your auto.docx (after deleting the extraneous paragraph breaks) - the table moved to the next page. Indeed, the code specifically inserts a manual page break (Chr(12)) to force it to happen. Of course, if you leave those unnecessary paragraph breaks in the documents (which I said you shouldn't), you may not get the desired results.