向报表添加表,并返回表示该表的 Shape 对象。
语法
expression。
AddTable
(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 将忽略 NumRows 和 NumColumns。 创建表时,它有一行一列,其中包含以下文本 使用“表数据”任务窗格构建表 (见图 1) 。 选择该表时,Project 将显示名为 “字段列表”的表数据任务窗格,默认情况下选中“ 任务 ”。 使用 “字段列表 ”任务窗格,可以手动将字段添加到报表中。
图 1. 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 方法可以向表添加字段
另请参阅
形状 对象形状 对象Application.Table 方法
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。