Partager via


Access the Ribbon at run time

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can write code to show, hide, and modify the Ribbon, and enable users to run the code from controls in a custom task pane, actions pane, or Outlook form region.

You can access the Ribbon by using the Globals class. For Outlook projects, you can access the Ribbons that appear in a specific Outlook Inspector or Outlook Explorer window.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for the following applications: Excel; InfoPath 2013 and InfoPath 2010; Outlook; PowerPoint; Project; Visio; Word. For more information, see Features available by Office application and project type.

Access the Ribbon by using the Globals class

You can use the Globals class to access the Ribbon in a document-level project or VSTO Add-in project from anywhere in the project.

For more information about the Globals class, see Global access to objects in Office projects.

The following example uses the Globals class to access a custom Ribbon named Ribbon1 and set the text that appears on a combo box on the Ribbon to Hello World.

Private Sub Access_All_Ribbons_Globals()
    Globals.Ribbons.Ribbon1.comboBox1.Text = "Hello World"
End Sub
private void Access_All_Ribbons_Globals()
{
    Globals.Ribbons.Ribbon1.comboBox1.Text = "Hello World";
}

Access a collection of Ribbons that appear in a specific Outlook Inspector window

You can access a collection of Ribbons that appear in Outlook Inspectors. An Inspector is a window that opens in Outlook when users perform certain tasks, such as creating e-mail messages. To access the Ribbon of an Inspector window, call the Ribbons property of the Globals class and pass in an Inspector object that represents the Inspector.

The following example gets the Ribbon collection of the Inspector that currently has focus. This example then accesses a Ribbon named Ribbon1 and sets the text that appears on a combo box on the Ribbon to Hello World.

Private Sub Access_Ribbons_By_Inspector()
    Dim ribbonCollection As ThisRibbonCollection = Globals.Ribbons _
        (Globals.ThisAddIn.Application.ActiveInspector())
    ribbonCollection.Ribbon1.ComboBox1.Text = "Hello World"
End Sub
private void Access_Ribbons_By_Inspector()
{
    ThisRibbonCollection ribbonCollection = 
        Globals.Ribbons
            [Globals.ThisAddIn.Application.ActiveInspector()];
    ribbonCollection.Ribbon1.comboBox1.Text = "Hello World";
}

Access a collection of Ribbons that appear for a specific Outlook Explorer

You can access a collection of Ribbons that appear in an Outlook Explorer. An Explorer is the main application user interface (UI) for an instance of Outlook. To access the Ribbon of an Explorer window, call the Ribbons property of the Globals class and pass in an Explorer object that represents the Explorer.

The following example gets the Ribbon collection of the Explorer that currently has focus. This example then accesses a Ribbon named Ribbon1 and sets the text that appears on a combo box on the Ribbon to Hello World.

Private Sub Access_Ribbons_By_Explorer()
    Dim ribbonCollection As ThisRibbonCollection = Globals.Ribbons _
        (Globals.ThisAddIn.Application.ActiveExplorer())
    ribbonCollection.Ribbon1.ComboBox1.Text = "Hello World"
End Sub
private void Access_Ribbons_By_Explorer()
{
    ThisRibbonCollection ribbonCollection =
        Globals.Ribbons
            [Globals.ThisAddIn.Application.ActiveExplorer()];
    ribbonCollection.Ribbon1.comboBox1.Text = "Hello World";
}

See also