1

I have a custom tab in the ribbon in VSTO. The first time I open the excel sheet , the default tab is "Home" .I want my custom tab to be opened as a default when I open my excel sheet.Please tell me how to accomplish this.

Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
Sam
  • 11
  • 3

3 Answers3

1

I had the same issue and see this has gone unanswered. I'm using VSTO in Excel 2013. This is easily accomplished by adding code similar to this in the Load event handler of your custom ribbon class (subclass of Microsoft.Office.Tools.Ribbon.RibbonBase):

private void YourCustomRibbon_Load(object sender, RibbonUIEventArgs e)
{
    RibbonUI.ActivateTab("yourCustomTabName");
}

"yourCustomTabName" is the ControlId of the custom RibbonTab object. It can be found in the ControlId property when you have the Ribbon Tab open in the RibbonTab designer - just under the (Name) property.

MattPerry
  • 306
  • 1
  • 7
0

You have to use a timer to accomplish this, as the ribbon is loaded asynchronously and has no StartupTab property.

If you're using Excel 2007, you have to access the ribbon via the ribbon's IAccessible properties, which I describe in my answer to the question Select VSTO Custom Ribbon in Excel.

System.Timers.Timer tmr { get; set; }

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
    tmr = new System.Timers.Timer(500)
    {
        Enabled = true
    };
    tmr.Elapsed += new System.Timers.ElapsedEventHandler(RibbonActivateTimer);
}

private void RibbonActivateTimer(object source, System.Timers.ElapsedEventArgs e)
{
    var tab = this.Tabs.SingleOrDefault(c => ((RibbonTab)c).Label == "YourStartupTab");
    if (tab != null)  // check to see if ribbon tab contains the ribbon deal
    {
        if (double.Parse(Globals.ThisWorkbook.Application.Version) >= 14) //14 = xl2010
        {
            this.RibbonUI.ActivateTab(tab.ControlId.CustomId);
            DeRegisterTimer();
        }
    }
}

private void DeRegisterTimer()
{
    tmr.Dispose();
}
Community
  • 1
  • 1
Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
0

For recent Office, in YourRibbon.cs file, at event Load method put these lines:

    private void YourRibbon_Load(object sender, RibbonUIEventArgs e)
    {
        Globals.Ribbons.YourRibbon.RibbonUI.ActivateTab("YOUR_TAB_CONTROL_ID");
    } 
Richard
  • 1
  • 1