views:

93

answers:

1

Hi, just a quick question. I've got this table in a Word template which have two columns by default. Then I've got this button the user can press if he wants another column. The macro run inserts several text placeholders and formats certain things automatically. But what I want is some sort of routine which basically checks the number of columns in this table, and if there are two columns, the text typed in is automatically "Column 3" and if there are three columns there, the text should be "Column 4". Should be pretty simple if I can just find out how I can find the number of columns.

+2  A: 

A table object knows how many columns it has, just check the Columns.Count property.

ThisDocument.Tables(1).Columns.Count
Tahbaza
Thank you, really simple! :)But here's something I ran into. I use the code you present above. And this works fine when I open the template (.dotm) and run the macro. But, strangely, when I doubleclick the template, making the document a "Document 1" document based on the template, the count doesn't work. Right now I have 6 columns in the table, but is reads as 2. This does not happen when I open the template. Any known reason that this should happen? I only have one table in there as well.
Kenny Bones
Try using ActiveDocument rather than ThisDocument then. ThisDocument is a reference to the document the macro lives within while ActiveDocument refers to the document with focus. It sounds like after you generate a new doc from your template the ThisDocument reference is no longer what you're wanting.
Tahbaza