Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WORD VBA Splitting Tables on BREAKS

Status
Not open for further replies.

WBURKERT

Technical User
May 28, 2010
73
Gentlemen,

I have a finished document consisting of many tables, some tables are long and across page breaks and some tables are short and still cross page breaks. I would like to have a macro find tables that cross breaks - hard breaks (manually inserted; page or section), soft breaks (WORD inserted breaks) and put the part of table that goes past the break as a new table. The reason is that I want to put a text header saying "Table continued" on the top of a page when tables are a continuation. The finished document will grow and shrink tables each time it is produced so there a lot of soft breaks.
So do you think this is remotely possible? Basically I want to rebuild Tables. Thoughts, advice, sample code?

Thank-you gentlemen
 
Why not use repeating table heading rows? That way IF the table crosses a page break the continuing table has th esame heading row.

BTW: I would avoid using manually inserted page break. All in all they tend to create more trouble.

Because of the way Word handles "pages" - there is no such thing really - most of what you mention would be very difficult.

Gerry
 
Hi Gerry,

I am a glutten for punishment and a firm belive that you can do anything with software, being very difficult is exactly the challenges I like to undertake.

I have written a medium and a low level flowchart for this idea and will begin putting something together this evening.

The macro will 'repaginate' and then evaluate the first table for a break. If yes then count the rows to the page break and make that a new table and put the remaining rows as a second table then check and see if the new table breaks a page and if yes then count the rows to the page break and make that a seperate table and the remaining rows make another table. Once the first table is checked and complete I would do another 'repaginate' and look for the second table and check if it breaks a page. I have a 240 tables so it looks like a bunch of repaginations and checks.

I am just wondering if a table breaks a page if I can detect the page break before the end of the table. I know the overall number of rows in the table so counting to the page break and subtracting this from the total shouldn't too terrible.

I will start this evening trying to check tables for page breaks before I go much further.

I am up for the challenge while trying to produce a very nicely formatted hybrid catalog/pricelist. Anybody have other ideas or comments? They are always welcome.

Thanks in advance.

Billy B.
 
fumei said:
Why not use repeating table heading rows? That way IF the table crosses a page break the continuing table has th esame heading row.
What's wrong with this?
 


Yes, I have a comment.

Gerry is the consumate Word expert in these forums. When a feature is built-in and available and will accomplish the requirement, then to attempt a coded solution, is merely an academic excersize, and frankly a waste of time when it comes to getting the job done, except for the academics of learning. And since we do not address student postings, you might be left to figure it out on your own.

Feel free to share your discoveries.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The repeating header row is just that whereas my need is to have line of text outside the table indicating the table is a continuation. The built-in feature will not accomplish this task. My tables have no headers and if they did it would just be PN, DESCRIPTION and PRICE but I have concluded that that info is understood.

I appreciate your comment, Skip, but this is not academic exercise nor a waste of time. I am not an enrolled student either. This is the first time I sensed getting blown off because the task is too hard. So be it. . .

Can anybody tell me if I can detect a break before detecing the end of table, if the table crosses the break boundry?

I'll keep everyone up-to-date with my progress, maybe it will get some traction once I get started. I will continue to ask genertic how to questions that I can incorporate the answer into my macros. Gerry, Thank-you for the motivation.
 
I am sorry but...

"Can anybody tell me if I can detect a break before detecing the end of table, if the table crosses the break boundry?"

Cracked me up. Ah.....that is the crux of the matter, is it not? Your success on this rather hangs on the answer.


Good luck! No offence intended, but I am afraid you are not quite grasping the object model regarding tables in Word. Nor the overhead required to do all the repagination you MUST do (every time you make any change at all!).

Is what you are wanting to do impossible? Ummmmmmmm.......no.

However, it will will will will take a LOT of coding and processing and is - unless you can seriously convince me otherwise - an exercise in potential frustration beyond the value returned.

But, if you get it working, I guarantee you will know a lot more about how Word deals with ranges and tables!
and check if it breaks a page. I have a 240 tables so it looks like a bunch of repaginations and checks.
Those 240 wil take a while. Plus the very fact you say breaks a page indicates a misunderstanding. A table never breaks a page.


BTW: technically speaking, if you break up a table (making two tables) that crosses a page boundary then your statement "Table continued" is a falsehood.

From Words perspective the table is NOT continued. You may say it does, but this (as least from Word) is not true.

And that is the issue. You are thinking Word thinks the same way as you. It does not think at all. It has an object model. It is software. And no,
firm belive that you can do anything with software
that is a falsehood. You can not do anything with software.

Software MUST work within its object model. You can NOT make software do anything contrary to its object model. True, the more one understands the object model the more you can make software dance through hoops not normally used.

Which is why I say is this impossible. Ummmmmmmm...no.

I am trying to decide whether to give you some starting help, or not. Ok.

Make a table with enough text in the first cell that it flows onto the next page. Are we clear?

Row1 on page 4 and continues onto page 5.
Row2 is on page 5.

WHERE are you going to make a forced break? Because not only does a table flow onto a following page, but the cell range does as well.

Can you detect is a given row is on a following page (say from the page the table starts on)? Yes. Here is my starting hint/help.
Code:
Sub WhatAMess()
Dim r As Range
Dim tblStartPage As Long
Dim oTable As Table
Dim oTableRange As Range
Dim oRow As Row
Dim msg As String

' get the page os the start of the table
Set oTable = ActiveDocument.Tables(1)
Set oTableRange = oTable.Range
   oTableRange.Collapse 1
   tblStartPage = _
      oTableRange.Information(wdActiveEndAdjustedPageNumber)
' loop through each row checking if it is the same page
For Each oRow In oTable.Rows
   Set r = oRow.Range
   r.Collapse 1
   If r.Information(wdActiveEndAdjustedPageNumber) <> _
      tblStartPage Then
      msg = "Row " & oRow.Index & " is AFTER the page break."
      GoTo tellme:
   End If
Next
tellme:
MsgBox msg
End Sub

My tables have no headers and if they did it would just be PN, DESCRIPTION and PRICE but I have concluded that that info is understood.
Perhaps it is understood...then WHY do you need a "Table continued"?????? If your users are knowledgeable to understand the first, do you not think they can see/figure out the table has continued?

Hmmmm?

"I am a glutten for punishment "

Yes sir, you are. One part of me is giving you a bravo though. Good luck!

Gerry
 
I just want to be clear.

IF a cell extends beyond the page boundary, then...that is what happens. Part of the cell/row is on page X and part of it is on Page Y.

So WHAT is the criteria for making a manual break? You would have to take that cell and break it up. That is make that cell/row into separate rows, THEN make it into separate tables.

Gerry
 
I will report back in the AM.

Okay the continued table will not say Table Continued but more like the name of the Table Continued.

If a table spans three pages I would like to have the reader know the name of the table (which is a product line) on the top of each page where the table continues. All readers will know the PN, DESCRIPT and PRICE but some readers will need their hand held as they move through product lines. If anything, the presentation of the document will be more pleasing to the eye because not much is left to chance. And a little more importanly, these kinds of decisions come from the higher ups and I need to find out the probability and try and forecast the time required.

I really appreciate your assistance through all of this, the document is finished except for this look.

 
Table Name? What table name? Tables do not have names.
If a table spans three pages I would like to have the reader know the name of the table (which is a product line) on the top of each page where the table continues.
That is exactly what table heading rows do.

Row1 "Table Name Whatever"
Row2 PN_stuff DESCRIPT_stuff PRICE_stuff
Row3 PN_stuff DESCRIPT_stuff PRICE_stuff
Row4 PN_stuff DESCRIPT_stuff PRICE_stuff
Row5 PN_stuff DESCRIPT_stuff PRICE_stuff
Row6 PN_stuff DESCRIPT_stuff PRICE_stuff
----- page break ------
"Table Name Whatever"
Row7 PN_stuff DESCRIPT_stuff PRICE_stuff
Row8 PN_stuff DESCRIPT_stuff PRICE_stuff
Row9 PN_stuff DESCRIPT_stuff PRICE_stuff


Gerry
 
This is exactly what I do not want to do - I know all about reapeating rows on breaks.

I am making the macro do

1.0 Frequency Counter Instrumentation

Row1 PN_stuff DESCRIPT_stuff PRICE_stuff
Row2 PN_stuff DESCRIPT_stuff PRICE_stuff
Row3 PN_stuff DESCRIPT_stuff PRICE_stuff
Row4 PN_stuff DESCRIPT_stuff PRICE_stuff
Row5 PN_stuff DESCRIPT_stuff PRICE_stuff
Row6 PN_stuff DESCRIPT_stuff PRICE_stuff
----- page break ------
1.0 Frequency Counter Intrumentation (continued)

Row7 PN_stuff DESCRIPT_stuff PRICE_stuff
Row8 PN_stuff DESCRIPT_stuff PRICE_stuff
Row9 PN_stuff DESCRIPT_stuff PRICE_stuff

 
This is exactly what I do not want to do - I know all about reapeating rows on breaks."

Except your example is exactly the effect of repeating heading rows, with the addition of "continued".

I am afraid you are walking into a serious pain in the butt.



Let me see if i fully inderstand.

1.0 Frequency Counter Instrumentation

Row1 PN_stuff DESCRIPT_stuff PRICE_stuff
Row2 PN_stuff DESCRIPT_stuff PRICE_stuff
Row3 PN_stuff DESCRIPT_stuff PRICE_stuff
Row4 PN_stuff DESCRIPT_stuff PRICE_stuff
Row5 PN_stuff DESCRIPT_stuff PRICE_stuff
Row6 PN_stuff DESCRIPT_stuff PRICE_stuff

The text "1.0 Frequency Counter Instrumentation" is NOT in the table, it is an independent paragraph before the table.

Yes?




Good luck.

Gerry
 
Gerry,

You are correct. The text "1.0 Frequency Counter Instrumentation" and "1.0 Frequency Counter (continued) are independent paragrapgh's.

The layout is more like:

1.0 Frequency Counter Instrumentation
<blank line>
blah, blah, blah <carriage return>
<blank line>
Row1 PN_stuff DESCRIPT_stuff PRICE_stuff
Row2 PN_stuff DESCRIPT_stuff PRICE_stuff
Row3 PN_stuff DESCRIPT_stuff PRICE_stuff
Row4 PN_stuff DESCRIPT_stuff PRICE_stuff
Row5 PN_stuff DESCRIPT_stuff PRICE_stuff
Row6 PN_stuff DESCRIPT_stuff PRICE_stuff
----- page break ------
1.0 Frequency Counter Intrumentation (continued)
<blank line>
Row7 PN_stuff DESCRIPT_stuff PRICE_stuff
Row8 PN_stuff DESCRIPT_stuff PRICE_stuff
Row9 PN_stuff DESCRIPT_stuff PRICE_stuff
 
1.0 Frequency Counter Instrumentation
<blank line>
blah, blah, blah <carriage return>
<blank line>


blank line!



Aaaaaaaaaaaaaaaaacccccccccccccccckkkkkkkkkkkkkkkk!

THAT is breaking the first/cardinal rule of best-practice in Word. It is contrary to the design of the Word object model. I will simply stop here. I have created literally thousands of Word documents. Not a single one has a "blank line".

Not one. Anywhere. But I will stop here as the others here have heard me go into this rant many times.

Well, if you persist on trying to do what you seem to want to do with your tables, and that caption-y type text above each table...with none crossing page boundaries, you will either:

A) fail
B) hopefully really get to understand a fair amount about Word. At least regarding tables and ranges.

That is a hint. You are going to have to work with Ranges....a lot.

Gerry
 
Oh, and again if you persist, you will - hahahahahaha - most likely need some suggestions/hints/help along the way.

I am willing to assist you to some extent. I will play it by ear, as this will not be trivial, and it is YOUR mouthful that you have bitten off. It is not my problem if it is more than you can chew.

BTW: I do not think it a particularly good idea in the first place. Or at least it is not a good return on investment. Unless the purpose is to gain knowledge of how Word works. In which case, shrug, yeah, it could be worth it.

Gerry
 



Gerry, you did not fail. It was just a matter of time. ;-)

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Gerry,

I have been under the weather but starting looking the solution and it looks to very easy if I use the 'Selection.SplitTable' command.

Something like this

For Each oRow In oTable.Rows
Set r = oRow.Range
r.Collapse 1
If r.Information(wdActiveEndAdjustedPageNumber) <> _
tblStartPage Then
Selection.SplitTable
End If
Next

I have tried this yet because I am getting for a Doctor's visit in the AM but will soon. Once I split the table then I should be able to insert some test because it puts a new paragraph and does just what I was looking for. Thoughts, ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top