One of the great things about Windows is the ability that we have to create COM-based add-in's for the entire suit of Office 2000 applications. In this article, Amit shows us how to create an Outlook 2000 COM add-in using a pure Visual C ATL COM object. He starts by writing a basic functional COM add-in, and then adds standard user interface elements such as toolbars and menu items to Outlook. He finishes up by showing us how to respond to their events.
Building An Outlook COM Add-In With VC /ATL - Command and conquer (Page 4 of 7 )
In office applications, menus and toolbars are combined into a fully programmable collection called CommandBars. CommandBars are common sharable programmable objects that are exposed by all office applications as a part of it's object model.
CommandBars represent a unified mechanism through which individual toolbars and menu items can be added to the corresponding application. Each CommandBars collection comprises of individual CommandBar objects. Each CommandBar object again contains a collection of CommandBarControl objects, called CommandBarControls.
CommandBarControls represent a complex hierarchy of objects and sub objects that comprise its object model. A CommandBarControl can itself contain a CommandBar object, accessible through the CommandBar property of the control. Finally, each CommandBarControl object within the CommandBarControls collection of controls can be either a CommandBarComboBox (toolbar combo box), a CommandBarButton (toolbar button), or a CommandBarPopup (popup menu).
In our add-in, we'd like to add the following user-interface elements to Outlook:
2 toolbar buttons (with bitmaps) in a new toolband.
A new popup menu item (with bitmap) to the 'Tools' menu.
First off, we need to import the office and outlook type libraries into our project. To do this, open the project's stdafx.h file and add the following #import directives:
You may need to change the paths above to match the location where Microsoft Office is installed on your system
Now that we're all set, let's dig into some code. Firstly, the toolband and toolbar buttons.
In the outlook object model, the application object is at the top of the object hierarchy, representing the entire application. Through it's ActiveExplorer method, we get the explorer object that represents the currently active window. We will use the GetCommandBars method to get the CommandBars object that, as you know, is a collection of all of Outlook's toolbands and menu items. We simply call the add method of the CommandBars collection with relevant parameters to add a new toolband.
Adding new buttons to the toolband is as simple as getting the toolband's CommandBarControls collection and then calling its add method. Finally, we query the buttons for the CommandBarButton object that we'll use to set button styles and other button properties, such as caption, tooltip text, etc.
The code snippet to do everything mentioned above look like this: