共用方式為


Accessing the Ribbon at Run Time

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 application-level projects for the following applications: Excel 2013 and Excel 2010; InfoPath 2013 and InfoPath 2010; Outlook 2013 and Outlook 2010; PowerPoint 2013 and PowerPoint 2010; Project 2013 and Project 2010; Visio 2013 and Visio 2010; Word 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

Accessing the Ribbon by Using the Globals Class

You can use the Globals class to access the Ribbon in a document-level project or application-level 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";
}

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

Accessing 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

Tasks

Walkthrough: Creating a Custom Tab by Using the Ribbon Designer

Walkthrough: Updating the Controls on a Ribbon at Run Time

Concepts

Ribbon Designer

Ribbon XML

Ribbon Object Model Overview

Customizing a Ribbon for Outlook

Accessing a Form Region at Run Time

Other Resources

Ribbon Overview