Kenny Bones
Kenny Bones

Reputation: 5129

Determine number of columns in a table in Word using VBA

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.

Upvotes: 0

Views: 15118

Answers (2)

Tahbaza
Tahbaza

Reputation: 9548

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

ThisDocument.Tables(1).Columns.Count

Upvotes: 2

Todd Main
Todd Main

Reputation: 29153

This works:

Sub CountColumns()
    Dim d As Document
    Set d = ActiveDocument
    Dim t As Table
    Set t = d.Tables(1)
    Debug.Print t.Columns.Count
End Sub

Upvotes: 1

Related Questions