Shapes.AddTable 方法 (Project)

向报表添加一个表,并返回一个代表该表的 Shape 对象。

语法

expressionAddTable (NumRows,NumColumns,Left,Top,Width,Height)

expression:一个表示 Shapes 对象的变量。

参数

名称 必需/可选 数据类型 说明
NumRows 必需 Long 表格的行数。 创建表时忽略 NumRows
NumColumns 必需 Long 表格的列数。 创建表时忽略 NumColumns
Left 必需 Single 相对于报表窗格左侧,表的左边缘 (以磅) 为单位。
Top 必需 Single 相对于报表窗格顶部,表的上边缘 (以磅) 为单位。
Width 必需 Single 表的宽度(以磅为单位)。
Height 必需 Single 表的高度(以磅为单位)。
NumRows 必需 INT
NumColumns 必需 INT
Left 必需 FLOAT
Top 必需 FLOAT
Width 必需 FLOAT
Height 必需 FLOAT
名称 必需/可选 数据类型 说明

返回值

Shape

备注

当 AddTable 方法创建表时,Project 将忽略 NumRowsNumColumns。 创建表时,表包含一行和一列,其中包含文本 “使用表数据”任务窗格生成表 (请参阅图 1) 。 选择表时,Project 会显示名为“ 字段列表”的表数据任务窗格,默认情况下会选择 “任务 ”。 使用 “字段列表 ”任务窗格,可以手动将字段添加到报表。

图 1. AddTable 方法创建包含一行和一列的表

使用 AddTable 方法

示例

TestReportTable 宏创建图 1 所示的表,然后选择表以显示“表数据”任务窗格。

Sub TestReportTable()
    Dim theReport As Report
    Dim tableShape As shape
    Dim theReportTable As ReportTable
    Dim reportName As String
    Dim tableName As String
    Dim rows As Integer
    Dim columns As Integer
    Dim left As Integer
    Dim top As Integer
    Dim width As Integer
    Dim height As Integer
    
    reportName = "Table Report"
    Set theReport = ActiveProject.Reports.Add(reportName)
    
     ' Add the table.
    tableName = "Task information"
    rows = 0
    columns = 0
    left = 0
    top = 30
    width = 110
    height = 20
       
    ' Project ignores the NumRows and NumColumns parameters when
    ' creating a ReportTable.
    Set tableShape = theReport.Shapes.AddTable(rows, columns, _
                        left, top, width, height)
    
    tableShape.Name = tableName
    tableShape.Select
    Set theReportTable = tableShape.Table
    
    With theReportTable
        Debug.Print "Rows: " & .RowsCount
        Debug.Print "Columns: " & .ColumnsCount
        Debug.Print "Cell 1,1 contents:" & vbCrLf & vbTab; .GetCellText(1, 1)
    End With
End Sub

若要指定表的字段,请使用 UpdateTableData 方法。 在上一个宏中的 行 Set theReportTable = tableShape.Table 后面添加以下代码。 OutlineLevel 参数指定级别 1 的任务,并筛选出项目摘要任务。

    ' Set fields for the table.
    Dim fieldArray(1 To 6) As PjField
        
    fieldArray(1) = pjTaskName
    fieldArray(2) = pjTaskStart
    fieldArray(3) = pjTaskFinish
    fieldArray(4) = pjTaskPercentComplete
    fieldArray(5) = pjTaskActualCost
    fieldArray(6) = pjTaskRemainingCost
        
    theReportTable.UpdateTableData Task:=True, OutlineLevel:=1, SafeArrayOfPjField:=fieldArray

对具有四个任务的项目运行修改后的 TestReportTable 宏,这些任务具有各种完成百分比和资源成本值。 图 2 是表结果的示例。

图 2. UpdateTableData 方法可将字段添加到表

使用 UpdateTableData 方法更新表格

另请参阅

Shapes 对象Shape 对象Application.Table 方法

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。