Table.AutoFormat method throws exception when first parameter is of type TableStyle RRS feed

  • General discussion

  • I want to apply a custom table format to a table. If I decide to use Table.AutoFormat method, for the first parameter, the Word Developer Reference states that the Variant's underlying type can be a WdTableFormat constant, a WdTableFormatApply constant or a TableStyle object (I cannot quote the original text here because it's localized).

    Because I want to apply a custom format I set the parameter to a TableStyle object. At runtime, an exception is raised because the types would be missmatched. If I use the constants it operates well. I get the same error with PIA or VBA. The macro recorder records nothing in Word 2010 (based on the dialog AutoFormat). In Word 2007, it records some WordBasic stuff.

    This issue seems to be a secret not only in the Word reference but the whole internet.

    Last but not least I could use the Style property to apply the format but I would like to also to use the Table.UpdateAutoFormat method. This method may require the format by the Table.AutoFormat method be assigned before. Or does the Table.AutoFormatType property (read-only) get it's value also by setting the Table.Style property?

    Thanks for answers and opnions.

    • Changed type stdval Friday, March 30, 2012 9:35 PM No problem at the moment.
    Wednesday, March 28, 2012 10:24 AM

All replies

  • Table.Autoformat predates Table Styles and does not work with them. If you have a (custom) Table Style you should just be able to apply it to the Table.

    Friday, March 30, 2012 2:00 PM
  • So I cannot invoke Table.AutoFormat with a TableStyle object which is a property of my custom Style object of type wdStyleTypeTable (I have set the style up in the Word UI for a template to use it in code later on documents derived from it)? The Table.AutoFormat method corresponds to the the AutoFormat dialog. In this dialog I can apply my style to a table. However, the same does not funtion with the method in code.

    Based on your answer, the Word Reference Documentation needs to be updated because it does not work with TableStyle objects:


    Note: I have changed the thread's type to Discussion because I applied my style with the Style property of the table, which works fine.

    Friday, March 30, 2012 9:35 PM
  • I don't know how to resolve the help reference, but what can I say? The documentation is wrong: this is hardly unusual. Sorry, I wish it were better, too. The reference to Table Style seems to have been added in Word 2007 Help and is not correct as written.
    That said, I'm not sure what you were wanting to do - Applying AutoFormats to Tables was all that was possible prior to the introduction of Table Styles in Word 2002, and since then it has primarily been maintained for legacy support. The Table Autoformat dialog is not available in the Ribbon (and has been removed from the legacy keyboard shortcuts) and you have to go out of your way to invoke it at all. If you want to use it in VBA you need to use the WordBasic method (as recorded in 2007) but I don't think UpdateAutoFormat really works at all in conjunction with Table Styles.
    As you have said you have switched to applying the style and that (a) does what you want and (b) is the 'right' way forward.

    Saturday, March 31, 2012 10:40 AM
  • Hi Tony,

    thanks for clarifying this. However, it is still possible to show the AutoFormat dialog via the User Interface: In a table, view the Format Explorer, where the table format is listed. When you click the table format name the AutoFormat dialog opens and you can still create new styles with it.

    Saturday, March 31, 2012 11:55 AM
  • Hello, stdval, I ran into the exact same problem and spent over 30 minutes trying to figure out what was wrong with my code, prior to finding this thread. Setting the table's Style property as you described seems to achieve the desired functionality, which is to apply everything about the style to the table, overriding what the user may have manually introduced.

    ' Apply the standard "Light List - Accent 1" style
    ' provided in Word 2007
    tInstance.Style = ActiveDocument.Styles("Light List - Accent 1")
    Thank you very much for explaining the problem and sharing the solution!
    • Edited by Marty Y. Chang Thursday, May 7, 2015 3:40 AM Expressed gratitude
    Thursday, May 7, 2015 3:39 AM