Hiding Groups on the Office Fluent Ribbon Using a Toggle Button
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
Summary: Performing various actions on the 2007 Microsoft Office Fluent user interface (UI), such as hiding groups, requires only a few lines of XML and programming code.
Applies to:2007 Microsoft Office System, Microsoft Office Excel 2007, Microsoft Office PowerPoint 2007, Microsoft Office Word 2007
Frank Rice, Microsoft Corporation
The 2007 Microsoft Office Fluent user interface (UI) replaces the current system of layered menus, toolbars, and task panes with a simpler system that is optimized for efficiency and discoverability. The Ribbon, context (right-click) menus, the Quick Access Toolbar, and the Microsoft Office Button are all parts of the Office Fluent interface. There are a number of custom and built-in controls, such as buttons, check boxes, and combo boxes, that you can add to the Office Fluent Ribbon. You add components to the Office Fluent Ribbon with XML markup elements, and set properties on those components by using attributes. You assign functionality to the components by using any programming language supported by Microsoft Visual Studio 2005, such as Microsoft Visual Basic .NET and Microsoft Visual C#, as well as Microsoft Visual Basic for Applications (VBA), Microsoft Visual C++, and Microsoft Visual Basic 6.0.
You can use a combination of XML and programming code to add your own custom controls to the Office Fluent Ribbon or perform actions on the Office Fluent Ribbon.
Adding Controls with XML
XML provides a hierarchical, declarative model of the Office Fluent Ribbon. Add controls, such as toggle buttons and tabs, to the Office Fluent Ribbon by using XML elements to specify the type of component. For example, you can add a single toggle button by using the toggleButton element. You assign property values to the control by using attributes such as the label attribute. The following is an example of the XML markup.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad" > <ribbon startFromScratch="false"> <tabs> <tab id="tab1" label="Hide Built-In Group Demo" > <group id="group1" label="Demo Group"> <toggleButton id="togglebutton1" imageMso="AcceptInvitation" label="Hide Window Group" onAction="flipGroupWindow" /> </group> </tab> <tab idMso="TabView"> <group idMso="GroupWindow" getVisible="groupWindowVisible" /> </tab> </tabs> </ribbon> </customUI>
This sample adds a custom tab named Hide Built-In Group Demo to the Office Fluent Ribbon by assigning text to the tab element's label attribute. This tab contains the Demo Group group, which contains a toggle button control named togglebutton1. The code includes another tab with the id of TabView. I'll discuss this tab in more detail shortly.
The togglebutton1 control has properties defined by using attributes such as imageMso, onAction, and label. You can assign these properties explicitly by setting the attribute equal to a String or indirectly by pointing to a programming code procedure. In addition to the label attribute, the toggle button also defines the imageMso attribute. This attribute points to an image built into Microsoft Office. You can determine this by the Mso suffix. Attributes with the Mso suffix always point to built-in functionality. To point to a custom image, you use the image attribute as in image="MyImage".
Next, is the onAction attribute. This attribute points to programming code called callback procedures. Callback procedures will be discussed in more detail in the Assigning Functionality to Office Fluent Ribbon Components section.
Finally, I already mentioned the TabView tab. In addition, I added a reference to the existing GroupWindow group. Because the tab and group use the idMso attribute, you know that these controls are pointing to a built-in tab and group, respectively; in this instance, the View tab and Window group that you see on the Office Fluent Ribbon in Office Excel 2007. However, by explicitly defining the controls this way, we now have access to all of the built-in functionality and can use them for our own purposes. So in this case, I override the visible property of the group by using the getVisible attribute to point to a callback procedure. You still have the functionality you normally see with the group, meaning all of the controls and commands that you would normally see, but you can take over the properties of the group. You will see how this works shortly.
Assigning Functionality to Office Fluent Ribbon Components
In the previous XML sample, the onAction and getVisible attributes point to callback procedures. When the toggle button is clicked, the onAction triggers the flipGroupWindow method, or callback procedure. The code in the flipGroupWindow method gives the toggle button its functionality. These procedures are called callbacks because when the button is clicked, the action alerts Microsoft Office that the control needs its attention. Microsoft Office then calls back to the method defined by the onAction attribute and performs whatever action is contained in the method. The following paragraphs describe these callback procedures in more detail.
The onAction attribute calls the flipGroupWindow callback procedure.
Private myRibbon As IRibbonUI Private groupWindowHidden As Boolean = False Public Sub OnLoad(ByVal ribbon As IRibbonUI) myRibbon = ribbon End Sub Public Sub flipGroupWindow(ByVal control As IRibbonControl, ByVal flip As Boolean) groupWindowHidden = flip myRibbon.Invalidate() End Sub
When the Office Fluent Ribbon is loaded, the OnLoad method is triggered, and a reference to the Office Fluent Ribbon is created and assigned to the myRibbon variable. In addition, a variable, named groupWindowHidden, that is used to trigger the visible state of the built-in group, is created. When the flipGroupWindow procedure is called by Microsoft Office, an IRibbonControl object representing the toggle button is passed in. In addition, a Boolean value representing the pressed state of the button is passed to the procedure. The procedure assigns the pressed state of the toggle button to the showTabView variable, and then calls the Invalidate method of the Office Fluent Ribbon. You will see the significance of these actions shortly.
Finally, the groupWindowVisible callback procedure for the GroupWindow group is called. This method returns a Boolean value that determines the visible state of the tab. In this case, the procedure returns a value that is the opposite of the pressed state of the toggle button.
Public Function groupWindowVisible(ByVal control As IRibbonControl) As Boolean Return Not groupWindowHidden End Function
To summarize, when the pressed state of the toggle button is True, meaning the button is displayed as depressed, the visible state of the group is False and the group is hidden. When the toggle button is displayed as not depressed, the group is visible.
There are two ways to deploy a custom Office Fluent Ribbon:
You can select the technique depending on the scope you need for the customized Office Fluent Ribbon. For example, modifying an Open XML file results in document-level customization where the customized Office Fluent Ribbon is associated with a particular document rather than the entire application. On the other hand, by using an add-in, you get application-level customization which means that the customized Office Fluent Ribbon applies to the entire application regardless of which document is open.
Creating a customized Office Fluent Ribbon using an Open XML file is not complicated.
To create a customized Office Fluent Ribbon using an Open XML file
Using an add-in to customize the Office Fluent Ribbon is equally simple. After creating the add-in project, you implement the IRibbonExtensibility interface, which is included in the Microsoft.Office.Core namespace. This interface contains a method called GetCustomUI. Use this method to return the XML Ribbon customization code to Microsoft Office. Then add programming procedures that give the custom Office Fluent Ribbon its functionality.
Hiding Tabs on the Office Fluent Ribbon
In the following procedure, you create a custom tab containing a custom group and toggle button that will hide or display the built-in Window group on the View tab on the Office Fluent Ribbon in Office Excel 2007.
Creating the Add-In Solution
There are nine steps to create the project.
To create the add-in solution adding custom buttons to the Office Fluent Ribbon
Visual Studio creates a solution that contains two projects: the add-in itself and a Setup project. The Setup project enables you to install the add-in on other users' computers, and it makes it easier for you, at design time, to install and uninstall the add-in.
Adding References to the Project
To interact with Office Excel 2007 and the Office Fluent Ribbon object model, add a reference to the Microsoft Office Core type library.
To add a reference to the project
Creating the Office Fluent Ribbon Customization XML File
Create the file that adds the components and sets the property for those components.
To create the Ribbon customization XML file
Adding the XML File as an Embedded Resource
For best results, use the XML file as a resource within the project's resource file.
To add the XML file as an embedded resource
Accessing the Host Application and Working with the Ribbon
You need to create an instance of Excel and add the Ribbon interface.
To access the host applications and work with the Office Fluent Ribbon
Testing the Project
Now you are ready to run the project.
To test the project
Length: 5:30 | Size: 3.38 MB | Type: WMV
There are a number of resources on customizing the Office Fluent user interface.