PDA

View Full Version : Solved: repeat table heading row?



eed
11-22-2005, 10:57 AM
Oookay, I'm sure this is something relatively simple, but it's eluding me all the same.

I'm running a piece of code that creates a Word table from a tab-delimited text file, then formats various aspects of the table. The table runs over several pages, which is fine, but I want the table heading row to repeat on each new page where the table runs. I can't seem to nail this down.

I checked the "Heading Rows Repeat" option on the Table menu, which is what the Word Help file instructs, but my table is (bewilderingly) unchanged, so I can't even capture an effective line of code with the macro recorder.

So what do I need to do to make the table's column headings repeat on new pages??

Thanks for any assistance!! http://vbaexpress.com/forum/images/smilies/pray2.gif
~ eed

TonyJollans
11-22-2005, 12:14 PM
Just a thought .. when you use Heading Rows Repeat you must have the heading rows selected so Word knows which rows they are.

In code you can do something like ...
TableRef.Rows(1).HeadingFormat = True
which is probably what you want if you are creating the table in code.

eed
11-27-2005, 06:37 PM
Tony,

Thanks for the suggestion, but the HeadingFormat line has no effect on my table; it still runs across three pages with the header row shown only on the first page. In the code formatting the newly created table, I entered:

With objTable
.Rows(1).Select
.Rows(1).HeadingFormat = True
' ... other formatting steps are here...
End With

All of my other formatting commands work like a charm, it's just this darn heading row. Any other ideas...? I appreciate the help!

~ eed

PS - Oh, by the way, I'm working with Office 2003, in case that matters... I'm actually running this code from Access to automate the document creation and formatting in Word. Thanks!

TonyJollans
11-28-2005, 04:57 AM
Hi eed,

Well - I can reproduce this and it looks like it fails when the table has a single row ...
This fails ...With Selection.Tables.Add(Selection.Range, 1, 2)
.Rows(1).HeadingFormat = True
End With
This works ...With Selection.Tables.Add(Selection.Range, 2, 2)
.Rows(1).HeadingFormat = True
End With
Sorry but I don't know why - assuming that there is any good reason that is.

fumei
11-28-2005, 07:46 AM
It fails even when you do it manually.

Make a single row table, make the single row a header row. Put some text in, and then make enough rows to carry onto the next page. There will no header row as the header row is NOT valid.

The Row collection must be > 1 for a HeaderRow to exist. Unfortunately there is no error raised when a table is created with a single row, with that row set as HeaderRow.

eed
11-28-2005, 08:05 AM
It fails even when you do it manually.

Make a single row table, make the single row a header row. Put some text in, and then make enough rows to carry onto the next page. There will no header row as the header row is NOT valid.

The Row collection must be > 1 for a HeaderRow to exist. Unfortunately there is no error raised when a table is created with a single row, with that row set as HeaderRow.

My table object starts with one row, then has an indefinite number of rows added to it depending on how many records are output from my Access query, so once the code is formatting it, it should have many rows. But maybe if I start with a two-row empty table before writing the records into it...? I'll give that a shot and let you know if it solves the problem.

Thanks!
eed

TonyJollans
11-28-2005, 10:31 AM
Hi eed,

Rather than artificially creating an extra row to begin with which might make your logic a bit more awkward, what about leaving the setting of the header row to the end at which point it should work?

eed
11-28-2005, 10:36 AM
Okay, I've finally got it under control now.

The code was inserting a tab-delimited text file into the Word doc, then converting it to a table, then adding a row at the top and formatting the new row as a repeating heading row before inserting the column names. But the row wasn't repeating.

I changed the order of operations so that the code inserts the column names, performs all the other formatting (auto-fitting the contents, etc.), THEN selects the first row and sets it to HeadingFormat = True as the last formatting action. For some reason, if it does this last, it works. Maybe this is connected to the failure with one-row tables that you guys described. Anyway, it works now, thank goodness.

Thanks for the assistance!!! http://vbaexpress.com/forum/images/smilies/023.gif
~ eed