How to customize context / right click menu of Excel 2013, 2010 and lower: C# sample
Right-click menus of Microsoft Office applications are a very convenient option for the end-user to interact with the host application. And for us, COM add-in developers, creating a custom menu for our users could be just a piece of cake. Honestly, I am not kidding… let’s check it out on an example of Microsoft Excel and you will see. In the next few minutes we will create our custom right-click menu that will work in all versions of Excel 2013, 2010, 2007, 2003 and lower – all with a single code base!
Office context menu – a bit of theory
When I say “a bit” of theory I really mean it :) There are two types of context menu (aka right-click or pop-up menus) and accordingly there are 2 ways of their customization:
- Command bar based context menus, which work in all Microsoft Excel versions from 2000 to 2013. Nope, this is not a misprint – Excel 2013 context menu can be customized in this old-fashioned way too. This context menu type is based on specific build-in command bars of the msoBarPopup type.
- Ribbon based context menus. These work only in the recent versions of Excel 2010 and 2013 and are based on the Office Fluent UI XML Schema.
Context menu types – commandbar vs. ribbon
Now let’s talk about advantages and drawbacks of both types.
Command bar context menu. The most obvious strength is the compatibility with all versions of Microsoft Office. The most serious limitation is that only two controls types can be added – command bar popup and command bar button.
Ribbon context menu. The main shortcoming is also obvious – only two MS Office versions are supported. And the most valuable asset is a rich set of controls to choose from: button, split button, check box, gallery, menu and menu separator.
Well, why not use both types in one project and leverage their advantages throwing off their weaknesses?
Creating a custom context menu for Excel 2013-2000
I am going to use Visual Studio 2012 and the new version of Add-in Express for Office and .net.
So, let’s start with creating a new COM add-in project using the Microsoft Office COM Add-in wizard. On the next step, select your programming language of choice – we choose C# for this sample, though if your preference is VB.NET or C++ you can select either one. After that specify the minimum supported Office version. It’s going to be Microsoft Office 2000 for our project because we want it to work in all versions – and the supported application, naturally it is Microsoft Excel.
Design a commandbar context menu
After creating the new COM add-in project in Visual Studio, right-click your AddinModule in Solution Explorer, choose View Designer and add a Context Menu component by clicking on the ADXContextMenu button on the toolbar.
Set the following properties of the newly created component to the following values:
Name = ExcelCommandBarContextMenu
SupportedApp = Excel
CommandBarName = Cell
Now let’s use Add-in Express’s visual designer to design our command bar context menu and add a couple of menu items to it. I am going to add a popup with 3 buttons and one separate button, set their captions, add images etc.
Create a ribbon context menu
Then add a Ribbon Context Menu component by clicking on the ADXRibbonContextMenu button on the toolbar.
Set the following properties of the newly created ribbon component to the following values:
Name = ExcelRibbonContextMenu
Ribbons = ExcelWorkbook
ContextMenuNames = Excel.ContextMenuCell, Excel.ContextMenuColumn, Excel.ContextMenuRow
For the ContextMenuNames property, there is a special editor where you can define all supported context menus.
Then on the visual designer surface, design your custom ribbon context menu. We will add the following menu items: a menu separator, a menu with 3 buttons and a standalone button, set their captions, images and event handlers:
And now we need to add an important bit of code – OnAddinInitialize event handler:
private void AddinModule_AddinInitialize(object sender, EventArgs e) { if (this.HostMajorVersion >= 14) // 14 == Excel 2010 { ExcelCommandBarContextMenu.UseForRibbon = false; } }
This code is necessary for the commandbar based context menu not to show up in Excel 2010 and 2013, as you remember we have added a ribbon context menu component for these versions.
Custom right-click menu in Excel: how it looks and works
Build the project, register and test it. Here are my test results:
Context menu in Excel 2003
Context menu in Excel 2007
Context menu in Excel 2010
Context menu in Excel 2013
That seems to be all. What we have got is both context menu types and support for all versions of Microsoft Excel in one project.
2 Comments
In Microsoft word, how can one make the context menu created using Addin Express show up in all context menu popups? For example, they show up over normal selected text and do not show up over text of a list item.
Hello Amitabh,
To solve the issue, try to customize the context menu “ContextMenuList”. This Id can be found in the lists of built-in Ribbon controls IDs, see section Referring to Built-in Ribbon Controls in the the PDF file in the folder {Add-in Express}\Docs on your development PC. The Ribbon context menu component allows specifying a number of context menus at once. That is, you can specify all required context menus in the ContextMenuNames property.