1

我在 VSTO 的功能区中有一个自定义选项卡。我第一次打开 excel 表时,默认选项卡是“主页”。我希望在打开我的 excel 表时默认打开我的自定义选项卡。请告诉我如何完成此操作。

4

3 回答 3

1

我有同样的问题,看到这个问题没有得到解决。我在 Excel 2013 中使用 VSTO。这可以通过在自定义功能区类(Microsoft.Office.Tools.Ribbon.RibbonBase 的子类)的 Load 事件处理程序中添加与此类似的代码轻松完成:

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

“yourCustomTabName”是自定义 RibbonTab 对象的 ControlId。当您在 RibbonTab 设计器中打开功能区选项卡时,可以在 ControlId 属性中找到它 - 就在 (Name) 属性下方。

于 2016-06-29T22:19:36.723 回答
0

您必须使用计时器来完成此操作,因为功能区是异步加载的并且没有StartupTab属性。

如果您使用的是 Excel 2007,则必须通过功能区的IAccessible属性访问功能区,我在对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();
}
于 2012-09-05T16:51:04.477 回答
0

对于最近的 Office,在 YourRibbon.cs 文件中,在事件 Load 方法中放置以下行:

    private void YourRibbon_Load(object sender, RibbonUIEventArgs e)
    {
        Globals.Ribbons.YourRibbon.RibbonUI.ActivateTab("YOUR_TAB_CONTROL_ID");
    } 
于 2021-01-19T14:30:23.007 回答