在執行時間存取功能區Access the Ribbon at run time

您可以撰寫程式碼以顯示、隱藏和修改功能區,並且讓使用者從自訂工作窗格、執行窗格或 Outlook 表單區域中的控制項執行程式碼。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.

您可以使用 Globals 類別來存取功能區。You can access the Ribbon by using the Globals class. 針對 Outlook 專案,您可以存取出現在特定 [Outlook 檢查] 或 [Outlook 總管] 視窗中的功能區。For Outlook projects, you can access the Ribbons that appear in a specific Outlook Inspector or Outlook Explorer window.

適用于: 本主題中的資訊適用于 - 下列應用程式的檔層級專案和 VSTO 新增 - 專案: Excel;InfoPath 2013 和 InfoPath 2010;展望PowerPoint方案Visio詞。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. 如需詳細資訊,請參閱 依 Office 應用程式和專案類型提供的功能For more information, see Features available by Office application and project type.

使用 Globals 類別存取功能區Access the Ribbon by using the Globals class

您可以使用 Globals 類別,從專案中任意位置存取文件層級專案或 VSTO 增益集專案中的功能區。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.

如需類別的詳細資訊 Globals ,請參閱 全域存取 Office 專案中的物件For more information about the Globals class, see Global access to objects in Office projects.

以下範例將使用 Globals 類別存取名為 Ribbon1 的自訂功能區,並且將出現在功能區下拉式方塊中的文字設定為 Hello WorldThe 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";
}

存取出現在特定 [Outlook 檢查] 視窗中的功能區集合Access a collection of Ribbons that appear in a specific Outlook Inspector window

您可以存取出現在 Outlook 檢查 器中的功能區集合。You can access a collection of Ribbons that appear in Outlook Inspectors. [檢查] 是一個視窗,會在使用者執行特定工作 (例如建立電子郵件訊息) 時,在 Outlook 中開啟。An Inspector is a window that opens in Outlook when users perform certain tasks, such as creating e-mail messages. 若要存取 [檢查] 視窗的功能區,請呼叫 Globals 類別的 Ribbons 屬性,並傳入代表 [檢查] 的 Inspector 物件。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. 然後這個範例會存取名為 Ribbon1 的功能區,並且將出現在功能區上下拉式方塊中的文字設定為 Hello WorldThis 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";
}

存取針對特定 Outlook Explorer 顯示的功能區集合Access a collection of Ribbons that appear for a specific Outlook Explorer

您可以存取出現在 Outlook Explorer 中的功能區集合。You can access a collection of Ribbons that appear in an Outlook Explorer. [總管] 是 Outlook 執行個體的主要應用程式使用者介面 (UI)。An Explorer is the main application user interface (UI) for an instance of Outlook. 若要存取 [總管] 視窗的功能區,請呼叫 Globals 類別的 Ribbons 屬性,並傳入代表 [總管] 的 Explorer 物件。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. 然後這個範例會存取名為 Ribbon1 的功能區,並且將出現在功能區上下拉式方塊中的文字設定為 Hello WorldThis 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