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 Office Fluent Ribbon, shortcut menus, the Quick Access Toolbar, and the Microsoft Office check box are all parts of the Office Fluent UI. 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 by using XML markup elements, and you 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 and Microsoft Visual C#, and also 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 other actions on the Office Fluent user interface.
Adding Controls Using XML
XML provides a hierarchical, declarative model of the Office Fluent user interface. You can add controls, such as buttons, to the Ribbon or to the Quick Access Toolbar by using XML elements to specify the type of component. For example, you can add a button by using the button element. You assign property values to the control by using attributes such as the label attribute. The following is a sample of the XML used to customize the Office Fluent UI.
<button idMso="Copy" />
<button idMso="Paste" />
This sample first sets the startFromScratch attribute of the ribbon element to true. Setting this attribute places the Office Fluent Ribbon in "start from scratch" mode, which hides all the built-in tabs. Next, I create a reference to the Quick Access Toolbar by adding the qat element to the code. Then I add the sharedControls element that is required to encapsulate any custom controls on the Quick Access Toolbar. And finally, I add two buttons, one for the built-in copy functionality and one for paste functionality.
You can tell that the control references functionality that is built into Microsoft Office through its use of the idMso attribute. Attribute names that include the Mso suffix always point to functionality and resources contained in the Microsoft Office product. Conversely, using the id attribute signifies a custom control.
There are two ways to customize the Office Fluent UI:
You can select the technique depending on the scope you need for the customized user interface. For example, modifying an Office Open XML Format–based file results in document-level customization where the customized Office Fluent Ribbon is associated with a particular document instead of the entire application. Alternatively, by using an add-in, you get application-level customization. This means that the customized Ribbon applies to the entire application regardless of which document is open.
Creating a customized user interface by using an Office Open XML Formats file is not complicated.
To create a customized Ribbon using an Office Open XML Formats file
Open the document as a compressed (.zip) file by changing the file name extension.
Add a folder that contains the XML customization code.
Modify the document's relationship file to point to the custom folder.
Change the document's file name extension.
Open the document in the Microsoft Office application.
Add code to the document to give the custom user interface its functionality.
Using an add-in to customize the user interface 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 customization code to Office. Then add programming procedures that give the custom user interface its functionality.
Customizing the Quick Access Toolbar
In the following procedure, you customize the Quick Access Toolbar by adding a button that provides the built-in copy functionality and a button that provides the built-in paste functionality in Office Excel 2007.
Creating the Add-in Solution
Follow these steps to create the project.
To create the add-in solution that adds custom buttons to the Ribbon
Start Visual Studio 2005.
On the File menu, point to New, and then click Project.
In the New Project dialog box, in the Project Types pane, expand Other Project Types, click Extensibility, and then select Shared Add-in.
In the Name box, type RibbonDemo, and then click OK to create the project.
On the first page of the Shared Add-in Wizard, click Next.
On the Select a Programming Language page, select either Visual C# or Visual Basic, and then click Next.
On the Select an Application Host page, clear all the selections except Microsoft Excel, and then click Next.
On the Enter a Name and Description page, optionally, type a name and description for the project, and then click Next.
On the Choose Add-in Options page, select I would like my Add-in to load when the host application loads, click Next, and then click Finish.
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. It also makes it easier for you, at design time, to install and uninstall the add-in.
Adding References to the Project
To interact with Microsoft 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
In Solution Explorer, expand the References folder.
If you do not see the References folder, on the Project menu, click Show All Files.
Delete the Microsoft.Office.Core reference.
Right-click the References folder, and then click Add Reference.
Click the COM tab, select Microsoft Office 12.0 Object Library, and then click OK.
At the top of the open code file, add the following statements to the project.
Imports Excel = Microsoft.Office.Interop.Excel
using Excel = Microsoft.Office.Interop.Excel;
Creating the Customization XML File
Create the file that adds the components.
To create the customization XML file
On the Project menu, click Add New Item.
In the Add New Item dialog box, select XML File. Name the new file Ribbon.xml, and then click Add.
In the new XML file, add the XML markup shown in the Code It section of this topic.
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
In Solution Explorer, select Ribbon.xml.
In the Properties window, select the Build Action property, and then select Embedded Resource in the list of options.
On the Project menu, click RibbonDemo Properties.
Click the Resources tab.
In Solution Explorer, drag Ribbon.xml onto the Resources design surface.
This action creates a file-based resource. From now on, the Ribbon.xml file is automatically stored as an application resource, and you can retrieve this content by using Visual Basic or Visual C# language features.
Close the Resources window. When prompted, click Yes to save the resources.
Accessing the Host Application and Working with the User Interface
Next, you create an instance of Excel and add a reference to the Ribbon extensibility interface.
To access the host applications and work with the Ribbon
In Solution Explorer, right-click Connect.cs or Connect.vb, and then click View Code.
Find the existing declaration for the applicationObject variable, and modify it so that it refers to an Excel.Application object. That is, modify the declaration so that it looks like the following code.
Private applicationObject As Excel.Application
private Excel.Application applicationObject;
Modify the existing first line of the OnConnection method, which creates an instance of the Excel.Application object.
applicationObject = DirectCast(application, Excel.Application)
In Visual Basic, modify the line of code, near the top of the class that starts with Implements, adding support for implementing the IRibbonExtensibility namespace. Visual Basic inserts the GetCustomUI procedure automatically.
Implements Extensibility.IDTExtensibility2, IRibbonExtensibility
If you are coding in C#, at the end of the public class Connect : statement, add a comma, and then type the following interface name.
Continuing in C#, right-click the interface you just added, click Implement Interface, and then click Implement Interface Explicitly. This adds a stub for the only IRibbonExtensibility interface member: GetCustomUI.
Modify the GetCustomUI method so that it looks like the following code.
Public Function GetCustomUI(ByVal RibbonID As String) As String _
string IRibbonExtensibility.GetCustomUI(string RibbonID)
Testing the Project
Now you are ready to run the project.
To test the project
On the File menu, click Save All.
Exit Excel 2007 if it is running.
On the Build menu, click Build Solution.
In Solution Explorer, right-click RibbonDemoSetup, and then click Build.
Right-click RibbonDemoInSetup, and then click Install.
The RibbonDemo Setup Wizard appears.
Click Next on each page, and then click Close on the last page.
Start Excel 2007.
Notice that because I set the startFromScratch attribute to true, no tabs are displayed on the Ribbon (see Figure 1). Also notice that the user interface that is displayed is the Microsoft Office Button (which cannot be hidden) and the Quick Access Toolbar containing two buttons.
Figure 1. No tabs are displayed on the Ribbon
Now type some text in cell A1, select the text, and then click the copy button (the left button) on the Quick Access Toolbar.
Next, click in cell A3, and then click the paste button (on the right) on the Quick Access Toolbar.
The text is pasted into cell A3, as shown in Figure 2.
Figure 2. Result of pasting the text from cell A1 to cell A3
In Visual Studio, in Solution Explorer, right-click RibbonDemoSetup, and then click Uninstall.
Watch the Video
Length: 7:30 | Size: 4.52 MB | Type: WMV
There are a number of resources on customizing the Office Fluent user interface.