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:
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()
:
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
You can use the ActivateTabMso
method to activate the ribon tab again:
this.RibbonUI.ActivateTabMso("TabAddIns");