Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Interop Excel C#: Check if ChartObject is selected

Posted on 2011-10-06
Medium Priority
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

Arno Koster earned 1000 total points
ID: 36924887
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

ID: 36929648
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)"

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

564 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