none
Getting first row of Table repeated at the top of subsequent pages RRS feed

  • Question

  • I have a VB (Visual Studio 2010) Console Application that uses Microsoft.Office.Interop.Word (Version 12) to create a Word Document (docx), based on a Template, using data retrieved from a database.  This does exactly what I want, with one exception.

    The Word Template contains the first row of a Table.  This first row contains the column headings.

    The application code adds rows to the table containing the data retrieved from the database.  This works as designed, including shading of certain rows etc.  Here's the statement that adds a row to the Table:

    WordSummaryCurrentRow = WordSummaryTableObject.Rows.Add()

    I want the first row in the Table to be repeated at the top of every page.  With the Template file Open in Word, I select the first (only) row, right click, select Table Properties..., select the Row tab and add a check mark to Repeat as header row at the top of each page  Then, in Table Tools, Layout, I click on Repeat Header Rows, so it is selected (highlighted), then save the Template file.

    I run the application, which correctly creates the additional rows in the table, but the first row is not repeated at the top of second and subsequent pages.

    Since making the setting in the Template did not have the desired effect, I added the following code, just before saving the created document:

    WordSummaryTableObject.Rows.Item(1).HeadingFormat =

     

    True

    WordSummaryTableObject.ApplyStyleHeadingRows =

     

    True

    The first row is still not repeated at the top of the second and subsequent pages.

    I moved the above two statements to the beginning of the code (before any rows are added), but the result is the same - the first row is not repeated at the top of the second and subsequent pages.

    With the created document open in Word 2010, with the Table selected, in Table Tools,, Layout, the Repeat Header Rows button is highlighted (selected).  If I click on it, the highlighting goes away.  If I click on it again, it gets highlighted again and now the first row is repeated at the top of each page.  So, this setting is on after the document is created, but it is not having the desired affect.

    What do I need to do (either in the Template or the application code) to get the first row repeated at the top of the second and subsequent pages?


    Bruce Sanderson
    Tuesday, December 7, 2010 1:01 AM

Answers

  • Thanks for your reply.

    Well, what appears to be the same code as I had before is now working as expected.  The first row is repeated at the top of every page.

    I removed the Repeat as header row at the top of each page in Table Properties, Row and Repeat Header Rows in Table Tools, Layout settings from the Template file.

    The code below is just before the document is saved:

              WordSummaryTableObject.Rows(1).HeadingFormat = True
              WordSummaryTableObject.ApplyStyleHeadingRows = True
    

    Bruce Sanderson
    • Marked as answer by bsanders Friday, December 10, 2010 11:48 PM
    Thursday, December 9, 2010 11:07 PM

All replies

  • Hello Bruce,

    Thanks for posting. I think you are looking for HeadingFormat property on Rows object. I got this property by recording a macro in my side. VBA code like this: "Tables(1).Rows.HeadingFormat = True". Also by searching on forums, I find that the topic about repeating header rows has been discussed before. For more information, please see these threads:

    Repeat as header row at the top of each page:
    http://social.msdn.microsoft.com/forums/en-us/vsto/thread/6B645A39-E538-4367-82E6-B4D2E426F69D.

    call a function when a new page in a word comes at runtime:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/4caaa801-5928-4dda-9f28-73212caabc61/.

    If you have any concern on this post, just feel free to follow up. Have a nice day.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, December 7, 2010 3:58 AM
  • Thanks for your reply.

    Well, what appears to be the same code as I had before is now working as expected.  The first row is repeated at the top of every page.

    I removed the Repeat as header row at the top of each page in Table Properties, Row and Repeat Header Rows in Table Tools, Layout settings from the Template file.

    The code below is just before the document is saved:

              WordSummaryTableObject.Rows(1).HeadingFormat = True
              WordSummaryTableObject.ApplyStyleHeadingRows = True
    

    Bruce Sanderson
    • Marked as answer by bsanders Friday, December 10, 2010 11:48 PM
    Thursday, December 9, 2010 11:07 PM
  • Hi Bruce

    <<Well, what appears to be the same code as I had before is now working as expected.  The first row is repeated at the top of every page.>>

    Sounds like the document is (or may have been) slightly damaged.

    If more oddities appear, it would probably be wise to recreate the template. One common way of doing this is

    1. Start Word in Safe Mode (hold down Ctrl)

    2. Save the new document (which has been generated from a clean copy of Normal.dotm due to starting in Safe Mode) as a template.

    3. Open the problem template and copy all EXCEPT the last paragraph mark (Ctrl+A, Shift+LeftArrow) to the new template.

    4. Recreate margins, headers, footers that will be lost due to leaving off the last paragraph mark.


    Cindy Meister, VSTO/Word MVP
    Friday, December 10, 2010 8:43 AM
    Moderator
  • Cindy: thanks for your post.  I recreated the Template using the technique you suggested.

    The first row of the Table is still only repeated at the top of each page if I remove the Repeat as header row at the top of each page in Table Properties, Row and Repeat Header Rows in Table Tools, Layout settings from the Template file and make the settings in the application code.

    I find this behaviour peculiar, but on the other hand, I'm consistently getting the results I require.


    Bruce Sanderson
    Friday, December 10, 2010 11:50 PM