將資料表新增到報表,並傳回代表資料表的 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 | |
| 名稱 | 必要/選用 | 資料類型 | 描述 |
傳回值
圖形
註解
當 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 方法可以將欄位新增至資料表
另請參閱
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。