WorksheetBase.ChartObjects Method
Gets an object that represents either a single embedded chart (a Microsoft.Office.Interop.Excel.ChartObject) or a collection of all the embedded charts (a Microsoft.Office.Interop.Excel.ChartObjects) on the worksheet.
Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)
Syntax
'Declaration
Public Function ChartObjects ( _
index As Object _
) As Object
public Object ChartObjects(
Object index
)
Parameters
index
Type: System.ObjectThe name or number of the chart. This argument can be an array, to specify more than one chart.
Return Value
Type: System.Object
An object that represents either a single embedded chart (a Microsoft.Office.Interop.Excel.ChartObject) or a collection of all the embedded charts (a Microsoft.Office.Interop.Excel.ChartObjects) on the worksheet.
Remarks
This method is not equivalent to the Charts property. This method returns embedded charts; the Charts property returns chart sheets.
Optional Parameters
For information on optional parameters, see Optional Parameters in Office Solutions.
Examples
The following code example demonstrates how to use the ChartObjects method to get the collection of embedded charts and a single embedded chart. The example first uses the ChartObjects method to get the Microsoft.Office.Interop.Excel.ChartObjects collection of the current worksheet and create a new Microsoft.Office.Interop.Excel.ChartObject. After formatting the new Microsoft.Office.Interop.Excel.ChartObject and giving the Microsoft.Office.Interop.Excel.ChartObject a name, the example then uses the ChartObjects method to get the new Microsoft.Office.Interop.Excel.ChartObject (indexed by its name) and displays a print preview of the chart.
This example is for a document-level customization.
Private Sub CreateAndPreviewChart()
Me.Range("A1", "A3").Value2 = 11
Me.Range("B1", "B3").Value2 = 55
Dim ChartObjects1 As Excel.ChartObjects = _
CType(Me.ChartObjects(), Excel.ChartObjects)
Dim chartObject1 As Excel.ChartObject = _
ChartObjects1.Add(100, 20, 400, 250)
chartObject1.Chart.ChartWizard(Me.Range("A1", "B3"), _
Excel.XlChartType.xl3DColumn, Title:="New Chart")
chartObject1.Name = "NewChartObject"
Dim chartObject2 As Excel.ChartObject = _
CType(Me.ChartObjects("NewChartObject"), Excel.ChartObject)
chartObject2.Chart.PrintPreview(False)
End Sub
private void CreateAndPreviewChart()
{
this.Range["A1", "A3"].Value2 = 11;
this.Range["B1", "B3"].Value2 = 55;
Excel.ChartObjects ChartObjects1 =
(Excel.ChartObjects)this.ChartObjects();
Excel.ChartObject chartObject1 = ChartObjects1.Add(100, 20, 400, 250);
chartObject1.Chart.ChartWizard(this.Range["A1", "B3"],
Excel.XlChartType.xl3DColumn, "New Chart");
chartObject1.Name = "NewChartObject";
Excel.ChartObject chartObject2 =
(Excel.ChartObject)this.ChartObjects("NewChartObject");
chartObject2.Chart.PrintPreview(false);
}
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.