Activate Ribbon Tabs in Office 2010 Solutions (Norm Estabrook)

  • Comments 5

Office 2010 now lets you activate tabs on the Ribbon. I gave this a try in a Outlook 2010 project and was very pleased with how easy it was to accomplish. Here are a couple of back-of-the-napkin examples that I chicken scratched over lunch.

From the Ribbon Load event

This code activates (puts in focus) the built-in Add-Ins tab when the user creates a new mail message. 

 [VB]

Public Class Ribbon1

    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        Dim mailItem As Microsoft.Office.Interop.Outlook.MailItem = _
            Globals.ThisAddIn.Application.ActiveInspector().CurrentItem

        If Not (mailItem Is Nothing) Then
            If mailItem.EntryID Is Nothing Then
                Me.RibbonUI.ActivateTabMso("TabAddIns")
            End If
        End If
    End Sub
.A
End Class

[C#]

public partial class Ribbon1
{
    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {
        Microsoft.Office.Interop.Outlook.MailItem mailItem = 
            Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
        
        if (mailItem != null)
        {
            if (mailItem.EntryID == null)
            {
                this.RibbonUI.ActivateTabMso("TabAddIns");
            }

        }
      
    }
}

From a Form Region

This code handles a button on an adjoining form region. The code activates a custom tab. Note that the only difference between this example and the previous one is that it uses the Globals class to access a Ribbon collection, and then gets the Ribbon of the active Inspector.

[VB]

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim RibbonCollection As ThisRibbonCollection = Globals.Ribbons _
    (Globals.ThisAddIn.Application.ActiveInspector())
        RibbonCollection.Ribbon1.RibbonUI.ActivateTab("MyCustomTab")

    End Sub

[C#]

private void button1_Click(object sender, EventArgs e)
{
    ThisRibbonCollection ribbonCollection =
        Globals.Ribbons [Globals.ThisAddIn.Application.ActiveInspector()];
    ribbonCollection.Ribbon1.RibbonUI.ActivateTab("MyCustomTab");
   
}

From Anywhere ..

So in honor of my favorite childhood author .. I will activate my Ribbon tabs with a mouse. I will activate them in a house.  I will activate them here or there. I will activate them anywhere.

Wherever you are in your project (task pane, class file, etc.),  just use the Activatexxx methods to activate your tabs. Oh and there is also a cool method named ActivateTabQ. However, I’ll leave that one for you. My lunch break is over :-)

Leave a Comment
  • Please add 3 and 5 and type the answer here:
  • Post
  • Hi

    asking for both VSTO 2008 , VSTO 2010

    is it possible to make the button in ribbon-bar visible from windows Foms ?

  • Hello,

    I am not sure about this. This blog focuses mainly on ways to automate Office by using Office add-ins. It sounds like you are attempting to automate Office features (such as the Ribbon) by using applications that run apart from Office.  To receive the best possible response, I recommend that you post this question to the Office Developer Forum - http://social.msdn.microsoft.com/Forums/en-US/categories.

    Norm E.

  • Cool... but I need this in.. Office 2007! Thnk McFly, think!

  • Cripes, no hotfix for office 2007 yet?

  • I have used XML Ribbon and used the getVisibale call back function, for example:

           public bool GetVisible( IRibbonControl control)

           {          

               if (Globals.ThisAddIn.Application.ActiveInspector().CurrentItem is Outlook.PostItem)

               {

                   Outlook.PostItem objPostItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook.PostItem;

                   return objPostItem.MessageClass.Equals("IPM.Post.My");

               }

               else

               {

                   return false;

               }

           }

    For more information see: msdn.microsoft.com/.../ee390805%28office.11%29.aspx

Page 1 of 1 (5 items)

Activate Ribbon Tabs in Office 2010 Solutions (Norm Estabrook)