Forum Discussion
Word VBA, Unreachable table style setting?
I've noticed something that may be a missing feature in the Word VBA Object model.
Styles in word can be of different kinds set by the WdStyleType when adding a style via expression.Add (Name, Type) method of the Styles Object (https://learn.microsoft.com/en-us/office/vba/api/word.styles.add).
Using
wdStyleTypeTable gives a Table Style.
Table styles differ somewhat from other styles since you build up the style of a table by setting the formatting for different parts/features of the table. E.g. the first, or last row may differ in their style compared to the rest of the table.
These kind of settings are reached via the Condition method of the TableStyle object (reachable from the Style.Table property).
Specifying different "conditions" (of data type WdConditionCode) give you access to ConditionalStyle objects handling the settings for different parts of the table.
In word you can modify the table styles by for example <right-clicking> a table style and selecting "Modify Table Style"
This presents you with the following UI.
Modify Style, Table edition.The setting I am unable to reach via VBA is tied to the Condition "First Row" which can be reached by selecting the pull-down menu "Apply formatting to:" to Header Row, then clicking the button menu "Format" at the lower left and selecting "Table Properties...".
This gives us the "Table Properties" UI in which we in the tab "Row" is given the options to enable "Repeat as header row at the top of each page".
The effect of this can be seen in the "Description field" in the previous "Modify Style (Table Edition)" view. (You might have to select something else in "!Apply formatting to:" menu and then re-select "Header row" again to get the description field to update).
We now have a "Repeat as header row" shown in the description for "Header rows".
This is a setting which in VBA can be reached for an inserted Table via a Rows property:
table.rows.HeadingFormathttps://learn.microsoft.com/en-us/office/vba/api/word.row.headingformat
However, it should be possible to set for the table style as well.
In the resulting XML file (styles.xml) of the .docx file we find it in a <w:trPr> block as a <w:tblHeader/> tag.
Note that this is the definition of a style in the styles.xml, not the declaration of an inserted table.
Have anyone managed to reach this style setting via VBA or know of how it can be done?
I've scanned through the methods and properties of Style, TableStyle, and ConditionalStyle object definitions and not found anything allowing me to set this "Table property" as part of the style. It would make most sense if it was available in the TableStyle object (https://learn.microsoft.com/en-us/office/vba/api/word.tablestyle) since that is where properties such as "AllowBreakAcrossPage" can be found.
Maybe something they missed in the Word VBA model.