Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-20-2023, 09:02 PM
soroush.kalantari soroush.kalantari is offline How to write a macro which automatically realize whether, after editing a document, a table can be p Windows 10 How to write a macro which automatically realize whether, after editing a document, a table can be p Office 2016
Competent Performer
How to write a macro which automatically realize whether, after editing a document, a table can be p
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default How to write a macro which automatically realize whether, after editing a document, a table can be p

When working with word documents, sometime I use “page break” to ensure a table or a picture don’t extend beyond one page(see the attacment1, if I didn’t use page break, the table1 last rows would go to page2). And it is often the case that I modify my previous Word documents (writing some new paragraphs, deleting some text) and such changes may make page break redundant. (see the attacment2, which is attachment 1th with its a 2th paragraph deleted. In this case the entire table1 can be put in page1) I want to write a macro which automatically realize such “redundant page breaks” and delete them. Can you give me some guides on how it can be done.
Attached Files
File Type: docx attachment1.docx (16.1 KB, 7 views)
File Type: docx attachment2.docx (15.9 KB, 3 views)
Reply With Quote
  #2  
Old 11-21-2023, 03:05 PM
gmaxey gmaxey is offline How to write a macro which automatically realize whether, after editing a document, a table can be p Windows 10 How to write a macro which automatically realize whether, after editing a document, a table can be p Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

What if your table is too big to fit on a page?


As long as each table will fit on a page, you could use something like this:

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
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-21-2023, 09:51 PM
soroush.kalantari soroush.kalantari is offline How to write a macro which automatically realize whether, after editing a document, a table can be p Windows 10 How to write a macro which automatically realize whether, after editing a document, a table can be p Office 2016
Competent Performer
How to write a macro which automatically realize whether, after editing a document, a table can be p
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default

thank you for your reply.
Regarding the “attachment 2”, the thing I want is to delete the “redundant page break” of page1th and fit the table on page 1th, (after deleting paragrah2th, the table can be fit on page1th). SO, there is no need to consider whether a table is small enough to be fit on one page.) I am not seeking a macro to ensure a table be fit on one page, but seeking a macro to delete redundant page breaks and delete the empty space between a table and its preceding paragraph, if the table can be fit on this space.
I اhave run your macro on “attachment 2”, but it does not delete the “redundant space”.
But your code did a good job in guiding me. I will study the concept like “keep with next” to understand whether I can modify your code to meet my particular need.
A way I have think of is to write a code to iterate among tables and go back to page break (by using Selection.Previous “command) and delete them and, by comparing the first and last row of tables, realize that selected table has been fit on one page or not and if the table has extended more than on page, insert a page break again.
Consider this code and its issues stated in the comments (not knowing how to determine if previous Paragraph is a page break and how to extend this code to include pictures):


Sub keeptables()

Dim oTbl As Table, tableindex As Integer, pagenumber As Integer, pagenumber2 As Integer

For Each oTbl In ActiveDocument.Tables
oTbl.Select


Selection.Previous(unit:=wdParagraph, Count:=2).Select 'beacude the tables has caption the count is 2
'I dont know how to determine the selected Paragraph is page break or not
Selection.Delete
oTbl.Rows(1).Select
pagenumber = Selection.Range.Information(wdActiveEndPageNumber)
oTbl.Rows(oTbl.Rows.Count).Select
pagenumber2 = Selection.Range.Information(wdActiveEndPageNumber)
If pagenumber <> pagenumber2 Then
Selection.InsertBreak Type:=wdPageBreak
Else
End If
Reply With Quote
Reply

Tags
page breaks, table



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a macro to count the number of lines between the start of a Word document to where the soroush.kalantari Word VBA 3 08-08-2021 09:23 PM
Protect from editing first page while having full editing capabilities in rest of document Waminathor Word 3 10-16-2019 08:22 AM
How to write a macro which automatically realize whether, after editing a document, a table can be p Automatically extract data from a table into another word document OfficeAssociate99 Word VBA 1 05-28-2017 11:19 PM
In Excel, how to automatically write a 3-attempt-exam final result in words? Abdulmunim Excel 9 04-21-2017 10:03 PM
Insert table in document - automatically updates second table in summary? Mechanic Word 2 08-01-2012 09:44 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:56 PM.


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