Interop Excel C#: Check if ChartObject is selected

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

scandinfoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arno KosterCommented:
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


or

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scandinfoAuthor Commented:
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)"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.