VSTO Word activate ribbon tab

ⅰ亾dé卋堺 提交于 2019-12-22 05:37:07

问题


I have the following ribbon.xml in my word vsto add-in:

<tab id="TabLetters" getVisible="IsLettersTabVisible" label="Letters">
 <group id="LettersGroup" label="Letters">
  <toggleButton id="NewWithTemplate"
              label="New using template Controls"
              size="large"
              imageMso="FileNew"
              onAction="NewTemplated" />
  </toggleButton>
 </group>
</tab>

And the following code behind the click event:

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");
}

I would have expected this to result in a new window with my ribbon tab opened, however it just remains the HOME tab that is visible/current. How do I make it happen that my tab is the one that is visible?


回答1:


Here are two ways you can use to set the active tab:

TabLetters.RibbonUI.ActivateTab("TabLetters"); or

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



回答2:


I found solution for excel 2007.

code :

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.
}



回答3:


In Excel 2013, here's the code I needed to use:

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

The bit I stumbled on was how to get the ControlID to pass to the ActivateTab function.

You need to open your MikesRibbon.cs file (or equivalent !) in VS2013. It'll show how your Ribbon will look, with a gray FILE tab next to your Ribbon's tab name.

In this Designer screen, click on your ribbon's tab (i.e. the tab to the right of FILE), and your Properties window will now show a ControlID value now, which you can set to a value of your choice.




回答4:


Just for all of you that have to support Office 2007 also (like me). Here's an (ugly, but working) solution for Office 2007:

  1. Open the office application
  2. Press ALT and then see the keyboard shortcut for your custom ribbon tab
  3. In your code you can now send this keys via the SendKeys.SendWait function

Hope it helps someone. Regards, Jörg


Code:

    public void FocusMyCustomRibbonTab()
    {
        if (IsExcel2007())
        {
            Globals.Ribbons.GetRibbon<MyRibbon>().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"));
    }



回答5:


In Word 2016 use RibbonUI.ActivateTabMso(controlID) to activate general Word Ribbon Tabs.

Additionally you can get the right reference to the Ribbon by adding in your AddIn:

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;
    }


来源:https://stackoverflow.com/questions/14627553/vsto-word-activate-ribbon-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!