Patrick
Patrick

Reputation: 383

VSTO Word activate ribbon tab

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?

Upvotes: 8

Views: 10094

Answers (6)

Mitselplik
Mitselplik

Reputation: 1133

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.

Upvotes: 0

Manfred Jehle
Manfred Jehle

Reputation: 1

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

Upvotes: 0

Hardik Shah
Hardik Shah

Reputation: 387

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

Upvotes: 3

Mike Gledhill
Mike Gledhill

Reputation: 29161

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.

Upvotes: 1

jreichert
jreichert

Reputation: 1566

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

Upvotes: 0

Denys Wessels
Denys Wessels

Reputation: 17029

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

Upvotes: 5

Related Questions