共用方式為


使用圖表

在 Word 2007 Service Pack 2 (SP2) 及更新版本中,您可以使用 Word 中的 VBA 物件模型,以程式存取和操作圖表。 Word 中的圖表物件也是使用 Excel 所用的共用 Office 繪圖層實作繪製的,因此,如果您熟悉 Excel 中的圖表物件模型,就可以將操作圖表的 Excel VBA 程式碼,輕鬆移轉至 Word VBA 程式碼中。

使用 Chart 物件

在 Word 中,圖表會由 Chart 物件表示。 Chart 物件會包含在 InlineShape 或 Shape 中。 使用 Document物件的InlineShapes集合或Shapes集合來新增或存取現有的圖表。 針對這兩個集合,您都要使用 AddChart 方法,指定圖表類型以及在文件中的位置,來新增圖表。

使用 HasChart 屬性來判斷 InlineShape 物件或 Shape 物件是否包含圖表。 如果 HasChart 傳回 True,您就可以使用 Chart 屬性來取得代表該圖表之 Chart 物件的參照。 目前為止,此實作與 Excel 的幾乎一模一樣,所以在大部分的情況下,VBA 程式碼可以在這兩個程式之間交換使用。

例如,下列 VBA 程式碼範例會新增一個平面堆疊直條圖至 Excel 中的使用中工作表,並將圖表的來源資料設定為 Sheet1 工作表的範圍 A1:C3。

Sub AddChart_Excel()
    Dim objShape As Shape
    
    ' Create a chart and return a Shape object reference.
    ' The Shape object reference contains the chart.
    Set objShape = ActiveSheet.Shapes.AddChart(XlChartType.xlColumnStacked100)
    
    ' Ensure the Shape object contains a chart. If so,
    ' set the source data for the chart to the range A1:C3.
    If objShape.HasChart Then
        objShape.Chart.SetSourceData Source:=Range("'Sheet1'!$A$1:$C$3")
    End If
End Sub

相較之下,下列 VBA 程式碼範例會新增一個平面堆疊直條圖至使用中文件,並將圖表的來源資料設定為與圖表相關聯之圖表資料的範圍 A1:C3。

Sub AddChart_Word()
    Dim objShape As InlineShape
    
    ' Create a chart and return a Shape object reference.
    ' The Shape object reference contains the chart.
    Set objShape = ActiveDocument.InlineShapes.AddChart(XlChartType.xlColumnStacked100)
    
    ' Ensure the Shape object contains a chart. If so,
    ' set the source data for the chart to the range A1:C3.
    If objShape.HasChart Then
        objShape.Chart.SetSourceData Source:="'Sheet1'!$A$1:$C$3"
    End If
End Sub

Word 中的 Chart 物件與 Excel 中的 ChartObject 物件之間的主要差異

雖然在大部分的情況下,在 Excel 中使用圖表的方式與在 Word 中幾乎一模一樣,但是了解兩者之間的主要差異很有幫助:

  • 在 Word 中以程式建立或操作 ChartData 物件時,需要執行 Excel。

  • 操作圖表的圖表屬性和方法並未實作。 圖表的概念僅適用於 Excel。 圖表並未在 Word 中使用,因此用於參照或操作圖表的方法和屬性已針對該些應用程式停用。

  • 在 Excel 中通常接受 Range 物件參考的屬性和方法,現在會在 Word 中取得範圍位址。 Word 中的 Range 物件與 Excel 中的 Range 物件不同。 為了避免混淆,Word 中的圖表物件模型可以在屬性和方法 (例如 Chart 物件的 SetSourceData 方法) 中接受範圍位址字串 (例如 "='Sheet1'!$A$1:$D$5"),這些屬性和方法也就是在 Excel 中接受 Range 物件的屬性和方法。

  • 新的 ChartData 物件已加入 Word 的 VBA 物件模型,以存取圖表的基礎連結或內嵌資料。 每一個圖表都有相關聯的資料,用於在 Word 中繪製該圖表。 圖表資料可以連結自外部 Excel 活頁簿,或內嵌在圖表的本身。 ChartData 物件會封裝 Word 中的特定圖表的資料存取。 例如,下列 VBA 程式碼範例會顯示及最小化 Word 中使用中檔所包含之每個圖表的圖表資料。

Sub ShowWorkbook_Word() 
    Dim objShape As InlineShape 
     
    ' Iterates each inline shape in the active document. 
    ' If the inline shape contains a chart, then display the 
    ' data associated with that chart and minimize the application 
    ' used to display the data. 
    For Each objShape In ActiveDocument.InlineShapes 
        If objShape.HasChart Then 
 
            ' Activate the topmost window of the application used to 
            ' display the data for the chart. 
            objShape.Chart.ChartData.Activate 
             
            ' Minimize the application used to display the data for 
            ' the chart. 
            objShape.Chart.ChartData.Workbook.Application.WindowState = -4140 
        End If 
    Next 
End Sub 

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應