使用 VBA 确定 Word 中表格中的列数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3382428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:57:41  来源:igfitidea点击:

Determine number of columns in a table in Word using VBA

vbams-wordword-vba

提问by Kenny Bones

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.

只是一个简单的问题。我在 Word 模板中得到了这个表格,默认情况下它有两列。然后我有了这个按钮,如果他想要另一列,用户可以按下。宏运行会插入多个文本占位符并自动格式化某些内容。但我想要的是某种程序,它基本上检查此表中的列数,如果有两列,则输入的文本自动为“第 3 列”,如果那里有三列,则文本应为“第 4 列”。如果我能找出如何找到列数,那应该很简单。

回答by Tahbaza

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

表对象知道它有多少列,只需检查 Columns.Count 属性。

ThisDocument.Tables(1).Columns.Count

回答by Todd Main

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