如何:向 Word 表添加行和列

在 Microsoft Office Word 表中,单元格被组织为行和列。 可以使用 Rows 对象的 Add 方法向表中添加行;使用 Columns 对象的 Add 方法添加列。

**适用于:**本主题中的信息适用于 Word 2007 和 Word 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

文档级自定义项示例

下面的代码示例可用于文档级自定义项。 若要使用这些示例,请从项目内的 ThisDocument 类中运行这些示例。 这些示例假设与您的自定义关联的文档已至少有一个表。

向表中添加行

  • 使用 Add 方法向表中添加行。

    Me.Tables.Item(1).Rows.Add()
    
    this.Tables[1].Rows.Add(this.Tables[1].Rows[1]);
    

向表中添加列

  • 使用 Add 方法,然后再使用 DistributeWidth 方法使所有的列具有相同的宽度。

    Me.Tables.Item(1).Columns.Add(BeforeColumn:=Me.Tables.Item(1).Columns(1))
    Me.Tables.Item(1).Columns.DistributeWidth()
    
    this.Tables[1].Columns.Add(this.Tables[1].Columns[1]); 
    this.Tables[1].Columns.DistributeWidth();
    

应用程序级外接程序示例

下面的代码示例可用于应用程序级外接程序。 若要使用这些示例,请从项目内的 ThisAddIn 类中运行这些示例。 这些示例假设活动文档已至少有一个表。

向表中添加行

  • 使用 Add 方法向表中添加行。

    Me.Application.ActiveDocument.Tables.Item(1).Rows.Add()
    
    this.Application.ActiveDocument.Tables[1].Rows.Add(
        this.Application.ActiveDocument.Tables[1].Rows[1]);
    

向表中添加列

  • 使用 Add 方法,然后再使用 DistributeWidth 方法使所有的列具有相同的宽度。

    Me.Application.ActiveDocument.Tables.Item(1).Columns.Add( _
        BeforeColumn:=Me.Application.ActiveDocument.Tables.Item(1).Columns(1))
    Me.Application.ActiveDocument.Tables.Item(1).Columns.DistributeWidth()
    
    this.Application.ActiveDocument.Tables[1].Columns.Add(
        this.Application.ActiveDocument.Tables[1].Columns[1]);
    this.Application.ActiveDocument.Tables[1].Columns.DistributeWidth();
    

请参见

任务

如何:创建 Word 表

如何:向 Word 表中的单元格添加文本和格式设置

如何:用文档属性填充 Word 表