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

发布于 2024-09-12 12:20:07 字数 190 浏览 2 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2024-09-19 12:20:07

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

ThisDocument.Tables(1).Columns.Count

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

ThisDocument.Tables(1).Columns.Count
清风不识月 2024-09-19 12:20:07

这有效:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文