How to find existing ChartObject in a worksheet?

I

itdept

My goal is to reuse the chartobject in a given sheet (ws).

int iCount = ws.ChartObjects.Count;
Excel.ChartObject myChartObject;

if( iCount > 0)
{
myChartObject = (Excel.ChartObject)ws.ChartObjects(iCount);
}
else
{
Excel.ChartObjects chartObjects =
(Excel.ChartObjects)ws.ChartObjects(missing);
myChartObject = chartObjects.Add(100, 100, 400, 300);
}
....

But this code couldn't compile due to:

Microsoft.Office.Interop.Excel._Worksheet.ChartObjects(object)' is a
'method', which is not valid in the given context

How could we detect if the existing ChartObject in a sheet? Could any
one help?

Thanks in advance for any hint.

--PD
 
I

itdept

My goal is to reuse the chartobject in a given sheet (ws).

int iCount = ws.ChartObjects.Count;
Excel.ChartObject myChartObject;

if( iCount > 0)
{
myChartObject = (Excel.ChartObject)ws.ChartObjects(iCount);}

else
{
Excel.ChartObjects chartObjects =
(Excel.ChartObjects)ws.ChartObjects(missing);
myChartObject = chartObjects.Add(100, 100, 400, 300);}

...

But this code couldn't compile due to:

Microsoft.Office.Interop.Excel._Worksheet.ChartObjects(object)' is a
'method', which is not valid in the given context

How could we detect if the existing ChartObject in a sheet? Could any
one help?

Thanks in advance for any hint.

--PD

Found solution:
Excel.ChartObjects chartObjects = (Excel.ChartObjects)
ws.ChartObjects(missing);
int iCount = chartObjects.Count;
Excel.ChartObject myChartObject;
if (iCount > 0)
myChartObject = chartObjects(iCount);
else
myChartObject = chartObjects.Add(...)
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top