View Single Post
 
Old 11-21-2023, 09:51 PM
soroush.kalantari soroush.kalantari is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Jun 2021
Posts: 124
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