Friday, November 19, 2021

Tables in Word documents opened in a thread

Earlier in the month, I wrote about opening Word in a background thread. Today I had reason to revisit the code, as the Occupational Psychologist wanted a few bells and whistles. The first request was to add a flag to cause the document to be displayed in landscape mode - simple. 

The second request was harder: should the document display a table, then that table should have the 'Repeat table header on subsequent pages' flag set. I know how to do this in a live document (I need it for my doctoral thesis) so it was quite easy to create a macro that would set the flag. Unfortunately, it wasn't clear which object within Word executes (Selection.Rows.HeadingFormat = wdToggle); is 'selection' a table? Is it something else? I had the idea to iterate over any tables that might be found within the Word document but not only was this difficult, it also didn't work.

I decided to leave this request for later, but serendipity played its part: one Word document that the management program created was now displayed horizontally, but more importantly, the table header repeated on the second and third pages! How did this happen, I asked myself, as I hadn't figured out how to do this yet. I looked at the HTML code that the program creates and that Word displays: along with the <table> tag, I had also used the <thead> tag. Somewhere along the line, I had defined a style in which the font in the header section is one colour, and the font in the <tbody> section is a different colour. It appears that this tag also causes Word to display the table with repeating header sections!  This saved me a great deal of head scratching.

No comments: