Dijeli putem


Ribbon overview

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

The ribbon is a way to organize related commands so that they are easier to find. Commands appear as controls on the ribbon. Controls are organized into groups along a horizontal strip at the top edge of an application window. Related groups are organized on tabs.

Most of the features that were accessed by using menus and toolbars in earlier versions of the Microsoft Office system can now be accessed by using the ribbon. For more information, see the technical article Developer overview of the user interface for the 2007 Microsoft Office system.

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.

Customize the Microsoft Office Ribbon

To customize the ribbon, add one of the following Ribbon items to your Office project:

  • Ribbon (Visual Designer)

  • Ribbon (XML)

    For example, to customize the Excel Ribbon, add a Ribbon item to an Excel VSTO Add-in project.

Ribbon (Visual Designer) item

The Ribbon (Visual Designer) item provides advanced tools that make it easier for you to design and develop a custom ribbon. Use the Ribbon (Visual Designer) item to customize the ribbon in the following ways:

  • Add custom or built-in tabs to a ribbon.

  • Add custom groups to a custom or built-in tab.

    Note

    A built-in tab or group is one that already exists on the ribbon of a Microsoft Office application. For example, the Data tab is a built-in tab in Excel. The Connections group is a built-in group on the Data tab.

  • Add custom controls to a custom group.

  • Add custom controls to the Backstage View.

    For more information about how to customize a ribbon by using the Ribbon (Visual Designer) item, see Ribbon designer.

Ribbon (XML) item

Use the Ribbon (XML) item if you want to customize the ribbon in a way that is not supported by the Ribbon (Visual Designer) item. Use the Ribbon (XML) item to customize the ribbon in the following ways:

  • Add built-in groups to a custom tab or built-in tab.

  • Add built-in controls to a custom group.

  • Add custom code to override the event handlers of built-in controls.

  • Customize the Quick Access Toolbar.

  • Share a Ribbon customization between VSTO Add-in by using a qualified ID.

    For more information about how to customize the ribbon by using the Ribbon (XML) item, see Ribbon XML.

Export a ribbon from the Ribbon Designer to Ribbon XML

If you create a ribbon by using the Ribbon Designer, and then decide that you want to customize the ribbon in ways that the Ribbon (Visual Designer) item does not support, you can export the ribbon to XML.

Visual Studio automatically creates a Ribbon (XML) item and populates the Ribbon XML file with elements and attributes for each control on the ribbon.

Not all of the properties that are in the Properties window of the Ribbon Designer are transferred to the Ribbon XML file. For example, Visual Studio does not export the value of the Image or Text property. That is because you must create a callback method in the Ribbon code file of the exported project to assign an image or set the text of a control. Visual Studio does not automatically generate callback methods as part of the export process.

In addition, any unchanged default property values do not appear in the resulting Ribbon XML file.

For more information about how to export the Ribbon to XML, see How to: Export a ribbon from the Ribbon Designer to Ribbon XML.

Update the code

A new Ribbon code file is added to Solution Explorer. This file contains the Ribbon XML class. You must create callback methods in the Ribbon Callbacks region of this class to handle user actions, such as clicking a button. Move your code from the event handlers to these callback methods and modify the code to work with the Ribbon extensibility (RibbonX) programming model. For more information, see Ribbon XML.

You must also add code to the ThisAddIn, ThisWorkbook, or ThisDocument class that overrides the CreateRibbonExtensibilityObject method and returns the Ribbon XML class to the Office application.

For more information, see Ribbon XML.

Add multiple Ribbon items to a project

You can add more than one Ribbon item to a single project. This is useful if you want to perform either of the following two tasks:

  • Create ribbons for Outlook Inspectors. For more information, see Customize a ribbon for Outlook.

    Note

    An Inspector is a window that opens when users perform certain tasks, such as creating an e-mail message.

  • Select which ribbon to display at run time.

Select which ribbons to display at run time

Because a project can contain more than one ribbon, you can select which ribbon to display at run time.

To select a ribbon to display at run time, override the CreateRibbonExtensibilityObject method in the ThisAddin, ThisWorkbook, or ThisDocument class of your project and return the ribbon that you want to display. The following example checks the value of a field named myCondition and returns the appropriate ribbon.

Note

The syntax used in this example returns a ribbon that was created by using the Ribbon (Visual Designer) item. The syntax for returning a ribbon that is created by using a Ribbon (XML) item is slightly different. For more information about returning a Ribbon (XML) item, see Ribbon XML.

Add the following code:

Protected Overrides Function CreateRibbonExtensibilityObject() As  _
    Microsoft.Office.Core.IRibbonExtensibility
    If myCondition = True Then
        Return Globals.Factory.GetRibbonFactory().CreateRibbonManager _
            (New Microsoft.Office.Tools.Ribbon.IRibbonExtension() _
                 {New Ribbon1()})
    Else
        Return Globals.Factory.GetRibbonFactory().CreateRibbonManager _
            (New Microsoft.Office.Tools.Ribbon.IRibbonExtension() _
                 {New Ribbon2()})
    End If
End Function
    protected override Microsoft.Office.Core.IRibbonExtensibility
CreateRibbonExtensibilityObject()
    {
        if (myCondition == true)
        {
            return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
                new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon1() });
        }
        else
        {
            return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
                new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon2() });
        }
    }
Title Description
How to: Get started customizing the ribbon Shows you how to customize the ribbon of a Microsoft Office application, add a Ribbon (Visual Designer) or Ribbon (XML) item to an Office project.
Ribbon designer Describes how you can use the Ribbon Designer to add custom tabs, groups, and controls to the ribbon of a Microsoft Office application.
Walkthrough: Create a custom tab by using the Ribbon Designer Shows you how to create a custom Ribbon tab by using the Ribbon Designer. You can use the Ribbon Designer to add and position controls on the custom tab.
Ribbon object model overview Provides an overview of the strongly typed object model that you can use to get and set the properties of Ribbon controls at run time.
Walkthrough: Update the controls on a ribbon at run time Demonstrates how to use the Ribbon object model to update the controls on a ribbon after the ribbon is loaded into the Office application.
Customize a ribbon for Outlook Provides guidance for customizing the ribbon in Microsoft Office Outlook.
Customize a ribbon for InfoPath Provides guidance for customizing the ribbon in Microsoft Office InfoPath.
Access the ribbon at run time Shows how 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.
How to: Change the position of a tab on the ribbon Shows how to change the order of tabs on a ribbon.
How to: Customize a built-in tab Shows how to add groups and controls to a built-in tab.
How to: Add controls to the Backstage View Shows how to add controls to the menu that opens when you click the File.
How to: Add a dialog box launcher to a Ribbon group Shows to add a dialog box launcher to any group on a ribbon.
How to: Export a ribbon from the Ribbon Designer to Ribbon XML Shows how to customize the ribbon in advanced ways by exporting the ribbon from the designer to Ribbon XML.
Ribbon XML Explains how you can customize a ribbon by using Ribbon XML.
Walkthrough: Create a custom tab by using the Ribbon Designer Demonstrates how to create a custom Ribbon tab by using the Ribbon (XML) item.