VSTO Word激活function区选项卡

我的单词vsto加载项中有以下ribbon.xml:

      

以及click事件背后的代码:

 public void NewTemplated(Office.IRibbonControl control, bool value) { CloseDocument(); var doc = Globals.ThisAddIn.Application.Documents.Add(Template: @"LETTER_V2.dotx", Visible: true); doc.Activate(); _ribbon.ActivateTab("TabLetters"); } 

我希望这会导致我的function区选项卡打开一个新窗口,但它仍然是可见/当前的HOME选项卡。 如何实现我的标签是可见的标签?

您可以使用以下两种方法设置活动标签:

TabLetters.RibbonUI.ActivateTab("TabLetters"); 要么

 Globals.Ribbons.CustomRibbon.Tabs[Your tab id].RibbonUI.ActivateTab("TabLetters"); 

我找到了excel 2007的解决方案。

代码:

 int appVersion = Convert.ToInt32(Globals.ThisAddIn.Application.Version.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0]); if (appVersion >= 14 ) { ThisRibbonCollection ribb = Globals.Ribbons; ribb.[Your Ribbon].ApplicationGroup.RibbonUI.ActivateTab("tab"); } else if(appVersion == 12) // Specific to Office 2007 only. { SendKeys.Send("%TAB%"); // use sendwait if you running it in thread. } 

在Excel 2013中,这是我需要使用的代码:

 try { // Attempt to set the my VSTO ribbon bar as the active ribbon. string controlID = Globals.Ribbons.GetRibbon().MikesTab.ControlId.ToString(); this.RibbonUI.ActivateTab(controlID); } catch { } 

我偶然发现的是如何让ControlID传递给ActivateTab函数。

您需要在MikesRibbon.cs打开MikesRibbon.cs文件(或等效文件!)。 它将显示您的function区的外观,并在function区的选项卡名称旁边显示灰色的“ FILE选项卡。

在此Designer屏幕中,单击function区的选项卡(即FILE右侧的选项卡),现在,“属性”窗口显示ControlID值,您可以将其设置为您选择的值。

仅适用于所有必须支持Office 2007的人(像我一样)。 这是Office 2007的一个( 丑陋但有效)解决方案:

  1. 打开办公室应用程序
  2. 按ALT键,然后查看自定义function区选项卡的键盘快捷键
  3. 在您的代码中,您现在可以通过SendKeys.SendWait函数发送此密钥

希望它可以帮到某人。 此致,Jörg


码:

  public void FocusMyCustomRibbonTab() { if (IsExcel2007()) { Globals.Ribbons.GetRibbon().tabMyRibbonTab.KeyTip = "GGG"; //Excel 2007: Must send "ALT" key combination to activate tab, here "GGG" SendKeys.Send("%"); SendKeys.Send("{G}"); SendKeys.Send("{G}"); SendKeys.Send("{G}"); SendKeys.Send("%"); } else { //Excel 2010 or higher: Build in way to activate tab if (this.ribbon.RibbonUI != null) { this.ribbon.RibbonUI.ActivateTab("MY_RIBBON_TAB_NAME"); } } } public static bool IsExcel2007() { return (Globals.ThisAddIn.Application.Version.StartsWith("12")); } 

在Word 2016中,使用RibbonUI.ActivateTabMso(controlID)激活常规Wordfunction区选项卡。

此外,您可以通过添加AddIn获得对function区的正确引用:

 static internal Microsoft.Office.Tools.Ribbon.OfficeRibbon rUI = null; private void WorkBenchRibbon_Load(object sender, Microsoft.Office.Tools.Ribbon.RibbonUIEventArgs e) { rUI = ((Microsoft.Office.Tools.Ribbon.OfficeRibbon)sender).Ribbon; }