Search code examples
c#exceloffice-interopoffice-addinsexcel-interop

How to prevent Excel from showing the Chart Design ribbon when programmatically switching sheets


I'm building an Add-in for Excel 365 and need to be able to programmatically switch the activated sheet of a workbook. This is easy and currently done using the following code:

sheet.Select();

Here's an example:

enter image description here

However, in case the sheet is a Chart (in the figure Chart1), and that chart is selected for the first time, Excel automatically switches from my custom Add-in Ribbon tab to the built-in Chart Design tab in the ribbon. By doing so, the user looses the custom tab, which is a usability issue for them.

Here's an example of how the ribbon looks like after I call sheet.Select():

enter image description here

Question: Is there a way to prevent Excel from switching the tabs, or otherwise, revert that change?

I tried calling ActivateTab, but upon invocation, that method always throws an ArgumentException with the following message:

Value does not fall within the expected range.

This is the code I use inside my Ribbon component:

var tab = this.Tabs.First();
var controlId = tab.ControlId;
var id = controlId.ToString();
this.RibbonUI.ActivateTab(id);// throws value does not fall within the expected range

Solution

  • You can use the ActivateTabMso method to activate the ribon tab again:

    this.RibbonUI.ActivateTabMso("TabAddIns");