PDA

View Full Version : keeping a table together on one page.



Zephid15
08-20-2007, 10:41 AM
My code is producing many small three row tables. I do not want the tables being split over the page breaks. is there a way in formatting that I can do this automatically?

Tommy
08-20-2007, 01:39 PM
Hi Zephid15,
When you create the tables set the .AllowPageBreaks = False
ex
Word.Tables(1).AllowPageBreaks = False

HTH

Zephid15
08-20-2007, 02:13 PM
The tables are still being devied over the page breaks. here is the code that i have:
Selection.TypeParagraph
Selection.TypeParagraph
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=21,
_DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
.Columns.PreferredWidth = InchesToPoints(0.35)
If .Style <> "QuickQuote" Then
.Style = "QuickQuote"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
.AllowPageBreaks = False
End With

Tommy
08-20-2007, 03:55 PM
With the code below in word 2003 this worked without placing any text in the cells. Well until I hit "QuickQuote" that was the first table then I reran and skipped the "QuickQuote" checks and the first one did not work but the second one did.
Sub aa()
Selection.TypeParagraph
Selection.TypeParagraph
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=21, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
.Columns.PreferredWidth = InchesToPoints(0.35)
If .Style <> "QuickQuote" Then
.Style = "QuickQuote"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
.AllowPageBreaks = False
.Rows.AllowBreakAcrossPages = False
End With


End Sub

Not trying to tell you what to do but I would have done this. I know Picky Picky, Picky :)
Dim TabFix As Table
Selection.TypeParagraph
Selection.TypeParagraph
TabFix = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=3, NumColumns:=21, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
With TabFix
.Columns.PreferredWidth = InchesToPoints(0.35)
If .Style <> "QuickQuote" Then
.Style = "QuickQuote"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
.AllowPageBreaks = False
.Rows.AllowBreakAcrossPages = False
End With

murphy84
09-03-2007, 12:27 AM
I know everyone here has said it's bad to use bookmarks - and perhaps it is but I've managed to solve almost every problem with them.

Here would be my example solution:-


For bCount = 1 To bTotal
bMarkName = ActiveDocument.Bookmarks(bCount).Name
If InStr(bMarkName, "tbl") > 0 Then
With ActiveDocument
.Bookmarks(bMarkName).Select
.Bookmarks("\EndOfSel").Select
End With
end_sel_page = Selection.Information(wdActiveEndPageNumber)

With ActiveDocument
.Bookmarks(bMarkName).Select
.Bookmarks("\StartOfSel").Select
End With
start_sel_page = Selection.Information(wdActiveEndPageNumber)

' If it's not on the same page
If Val(end_sel_page) <> Val(start_sel_page) Then
'Split this table here
Selection.InsertBreak Type:=wdSectionBreakNextPage
End If
End If
Next


You use check where the start and end of the range lies for the bookmark and if they are on different pages then you insert a page break before the table. You will need to mark each table with some sort of prefix or suffix to denote that the tables are to lie on a set page when bookmarking the tables.

Hope this helps!

fumei
09-03-2007, 09:02 PM
Not sure what the heck you are doing with the Selection. If I follow the code, it seems the last Selection was the bookmark. In any case, there is a huge whack of Selecting going on...NONE of which is needed.

What is the issue?

The issue is numbers.

What you REALLY want is to know if the page number of the start of the bookmarked table (a number) is less than the page number of the end of the bookmarked table (another number).

TWO numbers. So....use numbers. There is no need to Select the bookmark, then Select the start of the Selection. Select the bookmark again, and then Select the end of the Selection.

Bleeeeeech. Again, you want to deal with numbers. Using Selection is inefficient, and frankly irrelevant to the actual purpose.

I have questions about using a Section break before the table (rather than a page break), but that is your business. In any case, using numbers - no Selection:Dim oBM As Bookmark
Dim BMStart As Range
Dim BMEnd As Range
For Each oBM In ActiveDocument.Bookmarks
If InStr(oBM.Name, "tbl") > 0 Then
Set BMStart = oBM.Range
Set BMEnd = oBM.Range
BMStart.Collapse Direction:=wdCollapseStart
BMEnd.Collapse Direction:=wdCollapseEnd
If BMStart.Information(wdActiveEndAdjustedPageNumber) < _
BMEnd.Information(wdActiveEndAdjustedPageNumber) Then
BMStart.Move Unit:=wdCharacter, Count:=-1
BMStart.InsertBreak Type:=wdSectionBreakNextPage
End If
End If
NextNote also that there is no use of counters. The code uses a bookmark object and a For Each statement. Each bookmark (with "tbl") is tested for the page numbers.

As a possible alternative to explicitly inserting a Section break (why again???), you could set the property of the first row of the table to have a page break before.
' THIS
BMStart.Move Unit:=wdCharacter, Count:=-1
BMStart.InsertBreak Type:=wdSectionBreakNextPage
' could be replaced with this
oBM.Range.Tables(1).Rows(1).Range _
.ParagraphFormat.PageBreakBefore = True
Finally, by not using Selection, there is no need for the variables start_sel_page and end_sel_page.

fumei
09-03-2007, 09:11 PM
Oh, and....
I know everyone here has said it's bad to use bookmarks

I certainly don't know where you got that impression. I sure as heck do not feel that way. Bookmarks are good.

Bookmarked tables are VERY good.

murphy84
09-04-2007, 12:16 AM
Fumei,

Thanks for the advice above. It may prove very useful for trying to optimise my algorithms that perform the copying/tidying of the bookmarks en-masse. It seems ranges is the way forward!

However, whilst I'm grateful for your advice, please bear in mind that vba beginners like myself who may well have a knowledge of other languages, don't necessarily have the knowledge as far as optimisation and writing effective vba code is concerned. I've only been learning vba for several months and given the hefty deadlines I'm currently under was forced to get things working efficient or not! I'm sure other people who have been in previous situations like this can sympathise. Getting it working was no#1 priority - optimisation was a distant last!

Thanks again for your help - i'm sure it will prove useful.

fumei
09-04-2007, 12:36 AM
Oh, I do wish I had better diplomacy. Look, I am not trying to be critical. OF COURSE we all do things that just work. And we all start with not knowing how to make things work better.

I am not trying to be critical and make myself look great. I post these things as learning posts. To help people. I really, truly, want to help and assist people in knowing how things work.

Which is, for example, I pointed out the REAL purpose of what you want to happen.

You don't really want to Select the bookmarks again and again. You want to know if the darn table crosses a page, right? Which is a question of numbers, not selecting anything. So I pointed out how to do that.

While true, there IS a strong factor in my posts regarding optimization, the actual point is understanding. The more you understand how things work...."getting it to work" becomes easier.

That is what I try and do. Yeah, yeah, I know I am not the most diplomatic person, and I can get brusque, but that is not my intention.

My intention is to help as much as I can for anyone here who wants to know things I may know. Believe it or not, it is actually a sharing on my part.

It is one of the reason I discuss (...ummmm, rant) about understanding Ranges in Word. Once that light bulb actually goes on, "getting it to work" vastly expands. No matter what the task.

You can get it to work MUCH easier when you know how it works. So it really not just a question of optimization, it is understanding.

Tommy
09-04-2007, 11:42 AM
(...ummmm, rant)

Hi Gerry, :hi:

I so enjoy reading your "rants". I find them to be an indepth discussion of what to do and why. What more can you ask for? :dunno

fumei
09-04-2007, 12:08 PM
Whew, I am SO glad to hear that.

mdmackillop
09-05-2007, 04:06 PM
Hi Gerry, :hi:

I so enjoy reading your "rants". I find them to be an indepth discussion of what to do and why. What more can you ask for? :dunno
In full agreement. I find the Word VBA Help pretty poor and hard to navigate. I think I'm going to have to get a real book to help me get my head around Word coding.
Keep up the good work Gerry, much appreciated.