Consulting

Results 1 to 12 of 12

Thread: keeping a table together on one page.

  1. #1
    VBAX Regular
    Joined
    Feb 2007
    Location
    Illinois
    Posts
    68
    Location

    keeping a table together on one page.

    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?

  2. #2
    VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,168
    Location
    Hi Zephid15,
    When you create the tables set the .AllowPageBreaks = False
    ex
    [VBA]Word.Tables(1).AllowPageBreaks = False[/VBA]

    HTH

  3. #3
    VBAX Regular
    Joined
    Feb 2007
    Location
    Illinois
    Posts
    68
    Location
    The tables are still being devied over the page breaks. here is the code that i have:[vba]
    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
    [/vba]

  4. #4
    VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,168
    Location
    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.
    [VBA]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[/VBA]

    Not trying to tell you what to do but I would have done this. I know Picky Picky, Picky
    [VBA]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[/VBA]

  5. #5
    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:-

    [VBA]
    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
    [/VBA]

    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!

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:[vba]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
    Next[/vba]Note 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.
    [vba]' 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[/vba]
    Finally, by not using Selection, there is no need for the variables start_sel_page and end_sel_page.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  8. #8

    Thumbs up

    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.

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  10. #10
    VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,168
    Location
    (...ummmm, rant)
    Hi Gerry,

    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?

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Whew, I am SO glad to hear that.

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Quote Originally Posted by Tommy
    Hi Gerry,

    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?
    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •