Freigeben über


Customizing the Ribbon in Office 2007 Applications

One of the great new features in the Office 2007 application suite is the addition of the Office Ribbon.  While the ribbon provides most commonly used functions in an easy to navigate control, the true power of the ribbon comes in the ability for developers to customize and extend it.

Why is this important?  In OBA applications, you can now very easily create Word, Excel, even PowerPoint templates that prohibit functions such as New, Open, Save, even Print.

Just a few quick steps and you’ll see how easy and powerful these customizations are.

First, let’s start up a new Office 2007 project.  For this demo, I’m using Visual Studio 2008 SP1 and I’ll choose Word 2007 Template

1_NewOBAProject

After you press OK, a dialog window will ask you if you would like to create a new document or copy an existing.  For most cases, you will want to create a new document.  However, if you are using SharePoint and Content Types to create a Word document template with a document information panel…the second option will be of great value.

For the demo, I will choose the default Create a new document option.

 2_NewExistingDocument

After a moment, Visual Studio will bring up what looks like a Word 2007 document.  

What we want to do now is add the Ribbon XML configuration file which will be used to extend the out-of-box ribbon control.  There are two options here:  Ribbon (Visual Designer) and  Ribbon (XML) .  The visual designer has some limitations in what you can extend, so I always choose Ribbon (XML) .

4_AddRibbonXML

After pressing the Add button, you will notice two files are created in your project: Ribbon1.cs  and  Ribbon1.xml.  The Ribbon1.xml is the configuration file used to add, show, and hide controls on the ribbon.  Ribbon1.cs file is the extensible class used to load the xml configuration.

The xml file is the one where customization will reside, so open it up for edit.  Delete the default <tabs /> node and all sub-nodes under it.  Under the <ribbon> node, add the following xml:

<officeMenu>
     <control  idMso=”FileOpen”  visible=”false”  />
</officeMenu>

This configuration will hide the “Open” button from displaying on the menu.

5_ConfigRibbonXML

The last thing we need to do is tell our document to load our custom ribbon XML instead of the out-of-box ribbon.  To do this, open the ThisDocument.cs file by right-clicking and choosing View Code.

Add the override for the event to load our custom ribbon XML class.

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon1();
}

5_1_CreateRibbonEvent 

Note: if you open the Ribbon1.cs file, you will see a public method called “GetCustomUI” which returns a string reference to the xml file we just edited.

Finally, run your project in Debug and notice that the “Open” button is hidden.

 6_NoOpenMenuButton

For a complete list of extensible ribbon control id’s, download the Office 2007 Document List of Control IDs

Comments