Eugene Starostin

Customize Office toolbars, ribbons, task panes on RO Chrome

This post is the third part of my RemObjects Chrome and Add-in Express series. In part one I highlighted the key features of Add-in Express and showed how to get started with Outlook COM add-ins on RemObjects Chrome and Add-in Express. In part two, I explored such crucial for Office development concepts as security and deployment, RAD modules and visual designers . Today, in the final post of this series, I’ll dwell on the most important fields of Office customization: creating custom toolbars for traditional Office 2000 – 2007 user interface, customizing Ribbon UI in Office 2007 and adding application-level keyboard shortcuts.

Customizing Office

Due to the Microsoft Office extensibility and Add-in Express components you can customize the Office UI including toolbars, menus, sub-menus, ribbons, task panes and property pages. In this example I describe three most important fields of Office customization – extending the Office UI, accessing Office objects and handling their events.

Creating custom toolbars for traditional Office 2000 – 2007 UI
With the command bar components described in part 2 of my RemObjects Chrome series you can add your own or customize any existing toolbars and menus in your host applications. For example, to add a new toolbar to the Outlook Explorer window, open and right click on the Add-in Designer, and then select the Add Explorer CommandBar item. This adds an adxOlExplorerCommandBar component. Next, select the added component and customize it through the Properties window using the CommandBarName, Position, Protection, UseForRibbon properties. The FolderNames and ItemTypes properties are Outlook-specific; you can use them to bind your toolbar to certain Outlook folders or specified content. For example, select Mail in the ItemTypes property to show your toolbar for mail folders only (see the picture below).

Specifying Outlook Explorer command bar properties

All command bar components have the Controls collection which is a container for controls of you toolbar. Using its designer you can add to your toolbar standard Office toolbar controls such as button, edit and combo boxes, pop-ups, etc. To add a button to the toolbar, select the Controls collection on the Properties window and open its designer. Then, click the Add button, select ADXCommandBarButton and customize it.

Populating the command bar with controls

Then, create an event handler for the button click:


method AddinModule.adxCommandBarButton1_Click(sender: System.Object);
var
MailItem: outlook.MailItem;
begin
if OutlookApp.ActiveExplorer.Selection.Count > 0 then begin
MailItem := OutlookApp.ActiveExplorer.Selection.Item(1) as MailItem;
MessageBox.Show(MailItem.Subject);
end;
end;

Finally, register your project as an Office add-in. To do this, select the Register ADX Project item on the Build menu of your Visual Studio, run Outlook and click your button.

Third-party controls on Office toolbars
For Outlook, Excel, Word and PowerPoint you can use any third-party controls on Office toolbars. For example, let’s add a label that shows the subject of the selected mail.

First, you add to the add-in module a special component that supports third-party controls on Outlook toolbars. It is adxOutlookControlAdapter that you can find on the Toolbox; add it to the Add-in Designer. Next, add a label to the Add-in Designer and customize it through the Properties window.

Then, select your toolbar, open the Controls collection designer, click the Add button, select ADXCommandBarAdvancedControl and select the added label in the Control property.

Adding a custom control to the command bar

To show the subject of the selected mail we need to handle the SelectionChange event. To do this, right click on the Add-in Designer, select Add Events, click Outlook Events on the opened window and click OK. This adds the Outlook events component to the Add-in Designer.

Processing Outlook events

Using the Properties window, create an event handler for the ExplorerSelectionChange event:


method AddinModule.adxOutlookEvents_ExplorerSelectionChange(sender: System.Object; explorer: System.Object);
var
MailItem: outlook.MailItem;
begin
if OutlookApp.ActiveExplorer.Selection.Count > 0 then begin
MailItem := OutlookApp.ActiveExplorer.Selection.Item(1) as MailItem;
(Self.adxCommandBarAdvancedControl1.ActiveInstance as Label).Text := MailItem.Subject;
end;
end;

Finally, rebuild your project, run Outlook and select any mail. If you reproduce the steps above correctly, your label will show the subject of the selected mail item. Please note you needn’t register your project for the second time.

Your sample Outlook toolbar in action

Customizing Office 2007 Ribbon UI
Add-in Express includes special components for the Ribbon UI customization. To create your own ribbon tab, right click on the Add-in Designer and select Add Ribbon Tab. This adds an adxRibbonTab component to the Add-in Designer. Next, name your tab via the Caption property and specify its ribbons via the Ribbons property (for example, select OutlookMailCompose).

Creating a custom Ribbon tab

Then, add a group and button to the tab through the Ribbon Tab designer of the Controls collection.

Populating the Ribbon tab

Next, handle the click of the button:


method AddinModule.adxRibbonButton1_OnClick(sender: System.Object;
control: AddinExpress.MSO.IRibbonControl; pressed: System.Boolean);
var
MailItem: outlook.MailItem;
begin
MailItem := OutlookApp.ActiveInspector.CurrentItem as MailItem;
if Assigned(MailItem) then
MailItem.Subject := 'Test';
end;

Finally, rebuild your project, run Outlook, create a new mail and click your button.

Adding application-level keyboard shortcuts
With Add-in Express you can easily create application-level keyboard shortcuts. To do this, click on the Add-in Designer, set its HandleShortcuts property to true, right click on the Add-in Designer and select Add Keyboard Shortcut. An adxKeyboardShortcut will be added to the Add-in Designer. Next, set the shortcut text to the ShortcutText property of the add-in component (Ctrl-Shift-R for this example).

Intercepting a keyboard shortcut

Then, handle your shortcut action. In this example we reply to the currently selected mail and initialize its text:


method AddinModule.adxKeyboardShortcut1_Action(sender: System.Object);
var
MailItem: outlook.MailItem;
begin
if OutlookApp.ActiveExplorer.Selection.Count > 0 then begin
MailItem := OutlookApp.ActiveExplorer.Selection.Item(1) as MailItem;
MailItem := MailItem.Reply;
MailItem.Body := 'Test';
MailItem.Display(false);
end;
end;

Finally, rebuild your project, run Outlook and press Ctrl-Shift-R.

In this post I described only some of the features and capabilities that Add-in Express offers for customizing and enhancing your Office applications. To learn about other benefits provided by Add-in Express to .NET developers, visit Add-in Express home page

Related posts:

Make the Office 2007 Ribbon work easy
Create Office add-ins: ribbons, toolbars, task panes, menus
Create Microsoft Office extensions using RemObjects Chrome
Develop MS Office add-ins, smart tags, RTD servers on RO Chrome

Post a comment

Have any questions? Ask us right now!