|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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.
|
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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 |
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 |
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 |