Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Sub cmd_NoSplit()

    I wrote this for Word97 in despair after learning that Word97 had a KeepWithNext but STILL there was no KeepWithBefore.

    I maintain that the paragraph of text immediately before a table commonly needs to be kept with the table. ideally one would select the table and apply (Format, Paragraph, LineAndpageBreaks) "Keep With Before".

    I have just rewritten this procedure to take advantage of RANGE objects, so the code is perhaps juvenile.


    <pre>Public Sub cmd_NoSplit()
    ' Procedure : cmd_NoSplit
    ' Description: Keep all lines of a table together with the preceding paragraph.
    ' By: Chris Greaves Inc.
    ' Inputs: None.
    ' Returns: None.
    ' Assumes: None.
    ' Side Effects: Page layout may change.
    ' Tested: By a call from the user.

    ' We want to mark "Keep With next" for the paragraph of text preceding the table,
    ' and all rows but the last of the table.
    Dim rngKeep As Range ' Range object used to locate the necessary text.
    If Selection.Information(wdWithInTable) Then
    If Selection.Tables(1).Rows.Count > 1 Then_
    ' more than one row, so look for the penultimate.
    Set rngKeep = Selection.Tables(1).Rows(Selection.Tables(1).Rows. Count - 1).Range
    Dim lngEnd As Long
    lngEnd = rngKeep.End
    Set rngKeep = Selection.Tables(1).Rows(1).Range
    Else ' only one row, so deal with the first
    Set rngKeep = Selection.Tables(1).Rows(1).Range
    lngEnd = rngKeep.Start - 1_
    ' make the end to be the end of the paragraph of text.
    End If
    Dim lngStart As Long
    rngKeep.Start = rngKeep.Start - 1_
    ' go back one character into the preceding non-table paragraph.
    rngKeep.End = lngEnd
    rngKeep.ParagraphFormat.KeepWithNext = True
    Else
    Call errMustBeInTable
    End If
    End Sub
    </pre>


  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Attached is an updated version.

    NoSplitBefore: Place the text cursor in a table and then run this macro. The paragraph PRECEDING the table will remain with the table, avoiding any page boundary split.

    NoSplitBeforeAll: Will apply the procedure above to all tables in a document.

  3. #3
    Star Lounger
    Join Date
    Apr 2002
    Location
    Stone Ridge, New York, USA
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Hi Chris,

    I was so thrilled to see this post because I have a very similar problem I'm desperate to solve. What I need is for a table row to not separate from a text "row" falling directly below it. So, rather than the text preceding the table, in my case, the table precedes the text., and I need the table (which is always one row), to remain on the same page as the text that follows.

    Would it be possible to edit your code for this purpose? It would be very much appreciated.

    Thanks.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code: Sub cmd_NoSplit()

    The simplest way is to select the first cell of the table, then set "Keep with next" in the Line and Page Breaks tab of Format | Paragraph.

    If you want to use a macro:

    Sub KeepTableWithNext()
    If Selection.Information(wdWithInTable) Then
    Selection.Tables(1).Columns(1).Select
    Selection.ParagraphFormat.KeepWithNext = True
    Else
    Beep
    End If
    End Sub

  5. #5
    Star Lounger
    Join Date
    Apr 2002
    Location
    Stone Ridge, New York, USA
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Yes, one would think it would be that easy... But Keep with Next does not work to keep a table with a non-table paragraph of text. Thanks for the suggestion though.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Edited by HansV to break very long lines in <!t>[pre] ... <!t>[/pre] section

    Try Hans's code first, but bear in mind that MSWord is deficient in the user interface.
    Word2000 has (from the menu) Table, Tableproperties, Row, Allow row to break across pages.

    Word2000 VBA has this AND allowpagebreaks for a table, which the user interface does not.

    Here's a bit of VBA. Try doing what it does from the menu system. Hah hah!
    <pre>Public Function PageBreaksAllow(sel As Selection, lngSwitch As Long, blnAllow As Boolean)
    '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''
    ''' Function: tblPageBreaksAllow
    '''
    ''' Comments: Allow page Break (splits) in all tables that conform to the switch.
    '''
    ''' Arguments: SELECTION Selection provided by the caller.
    ''' LONG Switch to indicate TABLES fractional, entire, or both.
    ''' BOOLEAN TRUE if we allow breaks
    '''
    ''' Returns: None
    '''
    ''' Date Developer Action
    ''' --------------------------------------------------------------------------
    ''' 2006/03/23 Chris Greaves Created
    '''
    ''' Get the range. If no tables selected, use entire document
    Dim rng As Range
    Set rng = rngTables(sel)
    Dim tbl As Table
    For Each tbl In rng.Tables
    If (lngSwitch >= 0) And ((tbl.Range.Start >= rng.Start) And (tbl.Range.End <= rng.End)) Then
    ' table is entirely within the range
    <font color=red>tbl.AllowPageBreaks</font color=red> = blnAllow
    Else ' table is not entirely (is Fractionally) in the range
    If (lngSwitch <= 0) And Not ((tbl.Range.Start >= rng.Start) And (tbl.Range.End <= rng.End)) Then
    ' table is entirely within the range
    tbl.AllowPageBreaks = blnAllow
    Else
    End If
    End If
    Next tbl
    End Function</pre>


  7. #7
    Star Lounger
    Join Date
    Apr 2002
    Location
    Stone Ridge, New York, USA
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Chris, I'm running Word 2003. I'k not sure I explained my problem well enough. (Probably too exasperated!) I need to "attach" a one row table to a short paragraph of text so that the table row and the paragraph of text move together to the next page (and don't get split leaving the table row on one page and the text on the next).

    Unfortunately because of various reasons, I must have the table row followed by text not in a table, or I'd just set it up all in a table or all as text - but that's not possible here.

    Thanks for any further help.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code: Sub cmd_NoSplit()

    Keep with Next in the first cell of the last row of a table DOES keep that row on the same page with the non-table paragraph after the table - at least, it does in my Word 2002 SP-3.

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Code: Sub cmd_NoSplit()

    Sherry,

    Keep With Next certainly works the way I would expect on my PCs. If you select the end row of a table and set this paragraph attribute then that row remains with the next non-table paragraph. Setting the attribute on the last few rows of the table will cause the table to break just after the last row where you haven't set this. You may also need to clear the table row attribute "Allow row to break across pages".

    My PCs are all running Word 2003 SP2.

    StuartR

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Try this. It's not elegant, but it does the job and should get you started:<pre>Sub KeepLastRowWithNextParagraph()
    With Selection.Tables(1).Rows(Selection.Tables(1).Rows. Count)
    .Range.Paragraphs.KeepWithNext = True
    End With
    End Sub
    </pre>


    I think your task is the antithesis, if I can say that, of my "NoSplitBefore". NoSplitBefore's job was to glue/anchor the paragraph preceding a table to the table, and to keep the rows of the table together, that is, the preceding paragraph and all cells of the table all on the same page.
    If you wanted to glue/cement/bind so that all cells of the table and the following paragraph were always on the same table, you'ld want something as simple as:<pre>Sub KeepAllRowsWithNextParagraph()
    With Selection.Tables(1)
    .Range.Paragraphs.KeepWithNext = True
    End With
    End Sub</pre>

    which could be named "NoSplitAfter"

  11. #11
    Star Lounger
    Join Date
    Apr 2002
    Location
    Stone Ridge, New York, USA
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Okay, none of this works and I believe it's because I have to have a continuous section break between the table row and the paragraph text. Sorry I neglected to mention that from the start. So, I have the table row and then the continuous section break and then the paragraph text.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code: Sub cmd_NoSplit()

    Sheesh - you left out the most important piece of information.

    As far as I know, what you want is impossible - Word will not keep text before and after a section break together. Why is the section break essential?

  13. #13
    Star Lounger
    Join Date
    Apr 2002
    Location
    Stone Ridge, New York, USA
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    I need that section break in there so the table row repeats on the subsequent pages, but not on the page where the table row appears initially. It's complicated to explain, but I do need that section break there... or other things I need to work won't work. I'm hoping there's some code I can use to keep sections together. This "table row-section break-paragraph text" combination repeats numerous times throughout the form. This is a protected form with a complex layout.

  14. #14
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Code: Sub cmd_NoSplit()

    Sherry,

    I don't think that you can do this the way you are trying - but if you give us some more detailed information about what you are trying to do (from a Functional viewpoint, not a Word one) then we may be able to help you find a way that does work.

    StuartR

  15. #15
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_NoSplit()

    Sherry, which version of Word are you using? Because I just set up your scenario (table, continuous break, text para) in Word 2003, and KeepWithNext works perfectly, just as expected - section break and all.

    If you are using 2003 too, there must be something else that is causing it to fail.
    Beryl M


Page 1 of 2 12 LastLast

Posting Permissions

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