Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2011, 08:28 AM
SCMiller SCMiller is offline Keeping Table on One Page Windows Vista Keeping Table on One Page Office 2007
Novice
Keeping Table on One Page
 
Join Date: Apr 2011
Posts: 4
SCMiller is on a distinguished road
Default Keeping Table on One Page

I'm wanted to keep each table on one page. I'm missing something here. Can someone give me some help?



Code:
Sub KeepRowsTogether()
    Dim tbl As Table
    Dim nPara As Long, i As Long
    
    For Each tbl In ActiveDocument.Tables
        nPara = tbl.Range.Paragraphs.Count
        For i = 1 To (nPara - 1)
            tbl.Range.Paragraphs(i).KeepWithNext
        Next i
    Next tbl
End Sub
Reply With Quote
  #2  
Old 04-12-2011, 08:37 PM
macropod's Avatar
macropod macropod is offline Keeping Table on One Page Windows 7 32bit Keeping Table on One Page Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi SCMiller,

You need:
KeepWithNext = True.

However, the code could be greatly simplified:
Code:
Sub KeepRowsTogether()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
  oTbl.Range.Paragraphs.KeepWithNext = True
Next oTbl
End Sub
The problem with either approach, though, is that it forces the table to 'keep with next' with the first paragraph following it. What you need is for the last paragraph in each cell on the last row not to have the 'keep with next' attribute:
Code:
Sub KeepRowsTogether()
Dim oTbl As Table, oCel As Cell
For Each oTbl In ActiveDocument.Tables
  oTbl.Range.Paragraphs.KeepWithNext = True
  For Each oCel In oTbl.Rows.Last.Range.Cells
    oCel.Range.Paragraphs.Last.KeepWithNext = False
  Next oCel
Next oTbl
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-13-2011, 12:25 AM
SCMiller SCMiller is offline Keeping Table on One Page Windows Vista Keeping Table on One Page Office 2007
Novice
Keeping Table on One Page
 
Join Date: Apr 2011
Posts: 4
SCMiller is on a distinguished road
Default

Thanks! I greatly appreciate your help!
Reply With Quote
  #4  
Old 08-22-2016, 03:33 AM
andyroberts1234 andyroberts1234 is offline Keeping Table on One Page Windows Vista Keeping Table on One Page Office 2010 32bit
Novice
 
Join Date: Dec 2015
Posts: 2
andyroberts1234 is on a distinguished road
Default Vertically merged cells cause runtime error

Hello,

I have been trying to run this code however come across the following error:

Runtime error 5991: Cannot access the individual rows of this collection because the table has vertically merged cells.

Is there a way to make it work with tables with merged cells, or if not to do something like 'if table has merged cells then skip the formatting of this table'?

For clarity, this is the code I am running:

Code:
Sub ApplyTableAttribute()
'
' ApplyTableAttribute Macro
'
'
Dim oTbl As Table, oCel As Cell
' for each table in the active document
For Each oTbl In ActiveDocument.Tables
'set paragraphs to keep with next
  oTbl.Range.Paragraphs.KeepWithNext = True
  ' for each cell in the last row of the table
  For Each oCel In oTbl.Rows.Last.Range.Cells
  ' 'set the last paragraph to not keep with next
    oCel.Range.Paragraphs.Last.KeepWithNext = False
  Next oCel
Next oTbl
End Sub
Thanks

Andy
Reply With Quote
  #5  
Old 08-22-2016, 05:02 AM
macropod's Avatar
macropod macropod is offline Keeping Table on One Page Windows 7 64bit Keeping Table on One Page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub KeepRowsTogether()
Dim oTbl As Table, oCel As Cell
For Each oTbl In ActiveDocument.Tables
  With oTbl
    .Range.Paragraphs.KeepWithNext = True
    If .Uniform = True Then
      For Each oCel In .Rows.Last.Range.Cells
        oCel.Range.Paragraphs.Last.KeepWithNext = False
      Next oCel
    Else
      Set oCel = .Range.Cells(.Range.Cells.Count)
      Do While oCel.ColumnIndex > 1
        oCel.Range.Paragraphs.Last.KeepWithNext = False
        Set oCel = oCel.Previous
      Loop
      oCel.Range.Paragraphs.Last.KeepWithNext = False
    End If
  End With
Next oTbl
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Keeping Table on One Page Page Numbers in Table of Contents MerRob54 Word 6 01-28-2013 03:37 AM
Keeping Table on One Page Table Jumps to Next Page donthaveaclue Word Tables 3 02-25-2011 07:54 AM
Keeping Table on One Page table spanning full page datto210 Word Tables 1 10-25-2010 12:39 PM
Keeping Table on One Page Table rows moving to new page when you don't want them to graham Word Tables 1 11-16-2009 08:51 AM
Automatically recreate table on next page oneononemedia Word Tables 0 08-16-2009 08:24 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:41 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft