8

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?

Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
Patrick
  • 383
  • 1
  • 5
  • 13
  • Is this the exact contents of the `ribbon.xml`-file or just an example? If it is your file it is not valid XML, since the `toggleButton` tag is closed twice; first by `/>` and then by ``. – Olle Sjögren Jan 31 '13 at 14:22
  • I just made it a little bit smaller to display here. My xml is valid! – Patrick Jan 31 '13 at 14:34

6 Answers6

5

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");
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
3

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.
}
Hardik Shah
  • 387
  • 2
  • 11
1

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.

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
0

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"));
    }
jreichert
  • 1,466
  • 17
  • 31
  • I've changed the sent keytip into "GGG" to avoid possible keytip-conflicts with other Add-Ins. Background: The keytip used before was "Y2". But if you have for example "Adobe PDFMaker COM Addin" installed, then this keytip is already used.... – jreichert Jan 16 '15 at 10:00
  • With Excel 2013, using ActivationTab() with either the name or Tag of my VSTO ribbon causes a "Value does not fall within the expected range." exception. Dumb question: in VS2013, where do I find the "Tab Name" ? I can open my Ribbon.cs file in designer, and see Tag and (Name) in Properties... but no IDs.... – Mike Gledhill Dec 15 '15 at 12:54
0

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

If you came here looking for a solution because you have tried any of the above things from within a Application.DocumentOpen event handler or some similar event but keep getting an error, you may be running into this roadblock.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/baa9a81d-aad3-4af5-9d0a-f945c26ffa18/activiating-a-ribbon-tab-for-vsto-for-a-workbook-solutions-when-a-workbook-is-loaded-net-4?forum=vsto

The short and not-so-sweet answer is that you cannot activate the tab until...later...by using a Timer and respond to the elapsed event. Just hoping to save some folks time.

Mitselplik
  • 1,079
  • 1
  • 12
  • 16