Share via


WorksheetBase.ChartObjects(Object) Method

Definition

Gets an object that represents either a single embedded chart (a ChartObject) or a collection of all the embedded charts (a ChartObjects) on the worksheet.

public object ChartObjects (object index);
member this.ChartObjects : obj -> obj
Public Function ChartObjects (Optional index As Object) As Object

Parameters

index
Object

The name or number of the chart. This argument can be an array, to specify more than one chart.

Returns

An object that represents either a single embedded chart (a ChartObject) or a collection of all the embedded charts (a ChartObjects) on the worksheet.

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 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);
}
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

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.

Applies to