vba 如何使用VBA修改word中的表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5938770/
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-08 11:28:10  来源:igfitidea点击:

How to modify a table in word with VBA

vbams-wordword-2003

提问by mark

I have tables that I am creating, and I want to be able to modify them through the code in the VBA.

我有正在创建的表,我希望能够通过 VBA 中的代码修改它们。

What I need to do to the tables is merge and resize some cells and also add text to some of the cells.

我需要对表格做的是合并和调整一些单元格的大小,并向一些单元格添加文本。

回答by ray

To append on what Lance was saying, here's an example of Merging Cells and setting text in the value of those merged cells:

为了附加 Lance 所说的内容,这里有一个合并单元格并在这些合并单元格的值中设置文本的示例:

Dim myCells As Range
With ActiveDocument
    Set myCells = .Range(Start:=.Tables(1).Cell(1, 1).Range.Start, End:=.Tables(1).Cell(1, 3).Range.End)
    myCells.Select
End With

Selection.Cells.Merge


ActiveDocument.Tables(1).Cell(Row:=1, Column:=1).Range.Text = "Value for Merged Cells"

NOTE: The table in this example had three columns and two rows

注意:本例中的表格有三列两行

回答by Lance Roberts

You need to access the Table object, like

您需要访问 Table 对象,例如

ActiveDocument.Tables(1).Cell(Row:=2, Column:=2).Range.Text

or

或者

<some Word.Document here>.
  Content.Tables(1).Columns.SetWidth <columnwidthhere>, wdAdjustSameWidth