Interop Excel C#: Check if ChartObject is selected

Posted on 2011-10-06
Last Modified: 2012-05-12
I have made a custom Ribbon menu that sets our company formatting to charts. As the code works now it makes the same formatting for each chart object on one sheet. But now I need to format just one chart if it's selected.

The question is. How do I check if a chart object is selected and get a hold of it?

This is the code I'm using now:
private void SetColorHandler(object sender, RibbonControlEventArgs e)
                Excel.Workbook excelWorkBook = Globals.ThisAddIn.Application.ActiveWorkbook;
                Excel.Worksheet activeWorksheet = excelWorkBook.ActiveSheet;

                foreach (Excel.ChartObject chartObj in activeWorksheet.ChartObjects())
                              //Do something

Open in new window

Question by:scandinfo
    LVL 19

    Accepted Solution

    to see if a chart is selected, I would use an approach like this :

    excel.chart activeChart = excelworkbook.activechart;
    foreach (excel.chartobject chartobj in activeworksheet.chartobjects())
        if (chartobj == activeChart)

    Open in new window


    if TypeName(Globals.ThisAddIn.Application.Selection.Parent) == "Chart"

    Open in new window

    but as i currently have no VS installed i cannot check which will work.

    Author Closing Comment

    Thank you very much!

    I did go with the first code example. It works great with a minor change.

    Use "if (chartobj.Chart == activeChart)" instead of "if (chartobj == activeChart)"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Introduction This Article is a follow-up to my Mappit! Addin Article (, it was inspired by an email posting I made to EUSPRIG (, I will briefly cover: 1) An overvie…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now