我在VSTO的功能区中有一个自定义选项卡。我第一次打开excel表时,默认选项卡是“Home”。我想打开excel表时默认打开自定义选项卡。请告诉我如何完成此操作。
答案 0 :(得分: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属性中找到它 - 就在(名称)属性下。
答案 1 :(得分: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();
}
答案 2 :(得分:0)
对于最近的 Office,在 YourRibbon.cs 文件中,在事件 Load 方法中放置以下几行:
private void YourRibbon_Load(object sender, RibbonUIEventArgs e)
{
Globals.Ribbons.YourRibbon.RibbonUI.ActivateTab("YOUR_TAB_CONTROL_ID");
}