ChartObjects Method

WorksheetBase.ChartObjects Method (Object)

 

Gets an object that represents either a single embedded chart (a T:Microsoft.Office.Interop.Excel.ChartObject) or a collection of all the embedded charts (a T: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)

public object ChartObjects(
	object index
)

Parameters

index
Type: System.Object

The 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 T:Microsoft.Office.Interop.Excel.ChartObject) or a collection of all the embedded charts (a T:Microsoft.Office.Interop.Excel.ChartObjects) on the worksheet.

This method is not equivalent to the Charts property. This method returns embedded charts; the Charts property returns chart sheets.

For information on optional parameters, see Optional Parameters in Office Solutions.

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 T:Microsoft.Office.Interop.Excel.ChartObjects collection of the current worksheet and create a new T:Microsoft.Office.Interop.Excel.ChartObject. After formatting the new T:Microsoft.Office.Interop.Excel.ChartObject and giving the T:Microsoft.Office.Interop.Excel.ChartObject a name, the example then uses the ChartObjects method to get the new T: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);
}
Return to top
Show:
© 2016 Microsoft