PDA

View Full Version : modification of table header using VBA



r83
12-17-2008, 09:23 AM
When a table spans multiple pages, I would like to use VBA to insert a row saying "continued" at the top of the second page (I can then set this as the repeating table header to have it automatically repeat over the rest of the pages the table is on).

Using the tables collection, it is possible to iterate through the tables in a document and examine a given table and its contents but I can't work out a way to determine how many pages it spans, and where I would need to insert my new header cell to make it appear in the correct page.

Is this possible to do in VBA? There seems to be no simple way to access this information in word from VBA. :dunno :banghead:

Any ideas?

fumei
12-17-2008, 12:13 PM
Repeating table header is always Row 1 - that is, it is a repeat of the first row.

In other words, if a table spans THREE pages, there is no way to have "Continued" as the automated header row (on page 2 and page 3), and NOT have "Continued" as the first row on page 1.

Automated table header rows MUST include the first row of the table.

Further, automated table header rows ONLY work for automated page breaks. They do not work for manually inserted page breaks in the table. See Help.

So.

"When a table spans multiple pages, I would like to use VBA to insert a row saying "continued" at the top of the second page "

You do not need VBA. Just select the first row of the table and make it repeat by Table > Heading row repeat.

You can get information regarding the number of pages a table spans by making TWO range objects. One for the start of the table, one for the end of the table.

The reason you need two is that Range.Information(wdActiveEndPageNumber) is a property of the range itself. You can NOT get that information regarding the Range.Start, or Range.End.

Therefore...two separate ranges, one for the start of the table, one for the end of the table. Get the page number for both. Like this:
Option Explicit

Sub yadda()
Dim r1 As Range
Dim r2 As Range
' set both as the table range
Set r1 = ActiveDocument.Tables(1).Range
Set r2 = ActiveDocument.Tables(1).Range
' collapse one to the START
r1.Collapse 1
' collapse the other to the END
r2.Collapse 0

MsgBox "Table 1 starts on page " & _
r1.Information(wdActiveEndPageNumber) & _
" and ends on page " & _
r2.Information(wdActiveEndPageNumber) & _
"."
End Sub
This will display a message like:

"Table 1 starts on page 1 and ends on page 3."

Again, this probably does not help you if you want to do automated table header rows. Repeating header rows MUST contain Row1.

TonyJollans
12-18-2008, 01:51 AM
As Gerry says, repeated Table Header Rows are the same on every page of the table, including the first.

The, rather convoluted, way to do what you want is to put "continued..." into the header rows and then anchor a shape in the first non-header row and use it to cover up the text on the first page.

r83
12-18-2008, 02:26 AM
Thanks very much. :bow:

Rather than bodge a white shape over the (continued) text, my plan was to have the first row on page 1 contain "Table of blah", start a new section* in the table at the top of the second page then insert a new row containing "Table of blah (continued)".

This newly inserted row can then be marked as a repeating header and will be used for the following parts of the table only.

Of course I would need to be able to detect whereabouts in the table the page break has occured (haven't found a way to do this so far :bug: any ideas? :dunno )

* As if going to Insert > Break > Continuous

Thanks & regards

TonyJollans
12-18-2008, 05:57 AM
You can never be sure where a page break is going to occur unless you are using hard page breaks, which goes against the philosophy of using a word processor in the first place.

If you're creating a permanent document you could revisit the table when the document is complete (and then create a pdf from it). if you want to be able to change the document you will probably have to double check your table every time you amend the document.

fumei
12-18-2008, 11:50 AM
"This newly inserted row can then be marked as a repeating header and will be used for the following parts of the table only. "

No it can not. As stated, and here it is again, repeating header rows MUST - please read that - contain Row 1 of the table. The newly created row can not be marked as a repeating header.

The "newly created row" can not be marked as a repeating header for the "following parts of the table only".

BTW: have you actually tried inserting a Continuous Section break as you describe? Especially if the cell contents span the page break?

I understand why you want to do this, as it is a reasonable enough wish. However, as you have it now, it can not be done as you envision it.

As for "detect whereabouts in the table the page break has occured (haven't found a way to do this so far any ideas? )", this can be done, but unfortunately it may not help much. I am attaching a demo doc that does not do exactly that, but you could in theory adjust it to do so. Click "Whatever" on the top toolbar. Here is the code.
Option Explicit

Sub yadda()
Dim oTable As Table
Dim r As Range
Dim oCell As Cell
Dim j As Long
Dim msg As String
j = 1
Set oTable = ActiveDocument.Tables(1)
For Each oCell In oTable.Range.Cells
If oCell.ColumnIndex = 1 Then
Set r = oCell.Range
r.Collapse 1
msg = msg & "Row " & j & " starts on page " & _
r.Information(wdActiveEndPageNumber) & _
"." & vbCrLf
j = j + 1
End If
Next
MsgBox msg
End Sub



It displays a message box:

Row 1 starts on page 1
Row 2 starts on page 2
Row 3 starts on page 4

Notice that page 3 is NOT mentioned. That is because Row 2 starts on page 2 and ends on page 4. The cell has a lot of text in it, thus spaning TWO pages. How would you deal with that?

Further, the page break occurs within the content of the row, not conveniently at the start of a row. So...you want to insert a Continuous Section break at the top of a new page. Please do just that. Go to page 2 and Insert > Break > Continuous.

What happens?

r83
12-19-2008, 07:36 AM
Thanks for your reply, fumei.


No it can not. As stated, and here it is again, repeating header rows MUST - please read that - contain Row 1 of the table. The newly created row can not be marked as a repeating header.

Not sure why the method I suggested doesn't work for you. I've attached an example doc with a table formatted as I mentioned. Perhaps I explained it poorly.

As for the cells being split over a page - I should have explained. It is not a problem on this particular document as the cells are typically short, and should not be split over a page boundry anyway. There is still also a problem with users moving a table after it has been split.

EricFletcher
12-19-2008, 08:28 AM
I have used a page heading method that may work for you.

As Fumei points out, the first row repeat will only display a static image of whatever you put in it, and field codes in such a row are not changed dynamically. You can test this by putting a {PAGE} field in the 1st row: it would be nice if each new page displayed the updated value, but it doesn't; it uses only the page number for the page containing the first row. Unfortunately, this disqualifies use of calculated fields in a 1st row table header to get a continuation notice.

However, if your table is within a section, and if you set up page headers with Different first page, you can have an empty header for the 1st page, and a continuation header on subsequent pages. Set up the continuation header to "push" down into the text area if necessary to align the apparent top of the table. If your table has borders, it can be difficult to get the header cell's borders to align perfectly with the flow-over table rows, so I try to avoid layout designs that need vertical borders.

This method won't work for all circumstances of course, and does introduce potential complications of using sections. However, I have used it successfully in large documents with many flow-over tables.

EricFletcher
12-19-2008, 11:02 AM
Your sample file came in before my response went out. I've attached a demo file to show you what I mean. Examine the Table Properties for the 1?3 table in the header to see how the positioning was set up.

fumei
12-22-2008, 01:20 PM
"Not sure why the method I suggested doesn't work for you. "

I do know why. Because it does not work in the demo you attached.

Further, you obviously did NOT do as I suggested.
Further, the page break occurs within the content of the row, not conveniently at the start of a row. So...you want to insert a Continuous Section break at the top of a new page. Please do just that. Go to page 2 and Insert > Break > Continuous.

What happens?Or rather, you did, but you do not understand what you actually did.

Do you have Show/Hide on? If not, I suggest you do.

May I also suggest, in your demo document, put the cursor (Selection) in any row under the first "Abc".

Now go Table > Select > Table.

Now scroll down...

......does it look like the table on the next page - the one with "Abc (continued)" is still attached?

No, it is not selected. It is a new, separate and different table. So no...it does not work.

I will say it again. Repeating rows MUST have Row1. What you did was make a new table and used...ummmm....Row1 as the repeating header.

There is NO connection now between the table on page 2, and the table on page 1. They are not in the same table, and no, the header is not repeated from the first one.

Not only that, but try adding text into the first table, and let THAT push things to spill over into the next page.

r83
12-23-2008, 03:34 AM
I do know why. Because it does not work in the demo you attached.

There is NO connection now between the table on page 2, and the table on page 1. They are not in the same table, and no, the header is not repeated from the first one.


Yes, I see what you're saying.
It is obvious that this method is useless if it is applied whilst the document is being edited by the user. Moving the table or extending any of the rows would break it. However, I could apply it at print time, and restore it before control is returned to the user.
Yes it's a horrible fudge, but none of the other methods posted here (or found elsewhere on the web) work consistently either. It's surprising this is not a native option in Word to be honest.

EricFletcher
12-23-2008, 06:16 AM
. . . none of the other methods posted here (or found elsewhere on the web) work consistently either. It's surprising this is not a native option in Word to be honest.
Word is a text processor, not a layout program -- although the distinction is perhaps fuzzy with the many quite sophisticated formatting options available. In earlier versions, even creating a table that wasn't plain row & column was challenging: I still run into great looking tables that, when opened, reveal themselves to have been made by using tabs (or worse, spaces) between each line within cells -- just as you'd have to do with a typewriter. :doh:

Continuation table headers would undoubtedly be a nice feature to have for some users some of the time and a few users all of the time. But the reality is that most users would likely never use it. I'm sure this is on a "requested feature" list somewhere at Microsoft -- but I know it would rank lower than quite a few others on my personal wish list!

The workaround I suggested stems from a designer's take on the subject for a multi-volume project some years ago: she "forbid" any multi-page tables within the body, allowing only tables of 3/4 page or less and always positioned at the top or bottom of a page. Word can handle this with ease. Any tables that needed >1 page were relegated to the appendices section of the book. Since each such table then started a new page, my page header trick dealt easily with the table continuation issue.

Over the years I have found that working with a graphic designer early in a project's life cycle can overcome many of the limitations of the tools available. If they know that the tool can't easily do the task, they can often come up with alternatives -- and as the software "expert", I can give them feedback about potential technical challenges that a given design may pose.

TonyJollans
12-23-2008, 10:11 AM
Slightly off topic but I have suggested in the past that Tables in Word could benefit from a Header and Footer structure similar to that applied to Sections. As far as I can see this would provide enough flexibility for almost every request I've seen, whilst maintaining a familiarity (not that many people actually seem familiar with headers and footers <g>). I also think it would blend with Table Styles quite well, which already have distinct formatting for top row, bottom row, top left cell, etc.

Part of the problem (but only part) is that, as Word is not a layout program, tables tend to be used to enforce layout requirements - in much the same way as they were used for everything in HTML before CSS came of age.

fumei
12-23-2008, 11:24 AM
Excellent points Tony, and Eric.

I totally agree. From a programming perspective, I do not see that it would be all that difficult for Microsoft to incorporate a real header structure (like Sections) into table objects.

However, I suspect the reason they have not is that there is not enough demand (or understanding) for such functionality.

Tables used for layout? Absolutely. The comparison to tables in HTML is bang on.