添加表方法(Word)Tables.Add method (Word)

返回一个Table对象,该对象代表添加到文档中的新的空白表格。Returns a Table object that represents a new, blank table added to a document.

语法Syntax

表达式AddRangeNumRowsNumColumnsDefaultTableBehaviorAutoFitBehaviorexpression.Add (Range, NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior)

expression 是必需的。expression Required. 一个代表 "Tables" 集合的变量。A variable that represents a 'Tables' collection.

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
RangeRange 必需Required Range objectRange object 表格出现的区域。The range where you want the table to appear. 如果该区域未折叠,表格将替换该区域。The table replaces the range, if the range isn't collapsed.
NumRowsNumRows 必需Required LongLong 要在表格中包括的行数。The number of rows you want to include in the table.
NumColumnsNumColumns 必需Required LongLong 要在表格中包括的列数。The number of columns you want to include in the table.
DefaultTableBehaviorDefaultTableBehavior 可选Optional VariantVariant 设置一个值,该值指定 Microsoft Word 是否对表格中的单元格自动调整大小以适应单元格的内容(自动调整)。Sets a value that specifies whether Microsoft Word automatically resizes cells in tables to fit the cells' contents (AutoFit). 可以是下列常量之一: wdWord8TableBehavior ("自动调整" 禁用)或wdWord9TableBehavior (启用了 "自动调整")。Can be either of the following constants: wdWord8TableBehavior (AutoFit disabled) or wdWord9TableBehavior (AutoFit enabled). 默认常量是 wdWord8TableBehaviorThe default constant is wdWord8TableBehavior.
AutoFitBehaviorAutoFitBehavior 可选Optional VariantVariant 设置 Word 调整表格的大小的自动调整规则。Sets the AutoFit rules for how Word sizes tables. 可以是 WdAutoFitBehavior 常量之一。Can be one of the WdAutoFitBehavior constants.

返回值Return value

TableTable

示例Example

本示例在活动文档的开头添加一个 3 行 4 列的空表格。This example adds a blank table with three rows and four columns at the beginning of the active document.

Set myRange = ActiveDocument.Range(0, 0) 
ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4

本示例在活动文档的结尾添加一个 6 行 10 列的空白新表格。This example adds a new, blank table with six rows and ten columns at the end of the active document

Set MyRange = ActiveDocument.Content 
MyRange.Collapse Direction:=wdCollapseEnd 
ActiveDocument.Tables.Add Range:=MyRange, NumRows:=6, _ 
 NumColumns:=10

本示例向新文档中添加一个 3 行 5 列的表格,然后在表格的每个单元格中插入数据。This example adds a table with three rows and five columns to a new document and then inserts data into each cell in the table.

Sub NewTable() 
 Dim docNew As Document 
 Dim tblNew As Table 
 Dim intX As Integer 
 Dim intY As Integer 
 
 Set docNew = Documents.Add 
 Set tblNew = docNew.Tables.Add(Selection.Range, 3, 5) 
 With tblNew 
 For intX = 1 To 3 
 For intY = 1 To 5 
 .Cell(intX, intY).Range.InsertAfter "Cell: R" & intX & ", C" & intY 
 Next intY 
 Next intX 
 .Columns.AutoFit 
 End With 
End Sub

另请参阅See also

Tables 集合对象Tables Collection Object

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.