mercredi 8 juillet 2020

Why does pie chart COMobject have axes?

I'm trying to check if each chartObject I'm iterating over has a certain property (axes), and if so apply gridlines. Something like this, assuming the code would work:

foreach (ChartObject chartObject in chartObjects)
{
    yAxis = chartObject.Chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary)

    if ( yAxis != null)
    {
            yAxis.HasMajorGridlines = true; //Should enter for Line chart, not for Pie Chart
    }
}

My intuition tells me that during the iteration when the chartObject is a pie chart, yAxis should be null, because a pie chart has no axis. However, it seems like Axes is still a COMObject for the pie chart, and therefore is not null. That leads to it entering the if statement, trying to add gridlines and throwing a NullReferenceException.

Thanks, Fredrik





Aucun commentaire:

Enregistrer un commentaire