Home arrow C++ arrow Page 4 - Building An Outlook COM Add-In With VC /ATL

Building An Outlook COM Add-In With VC /ATL

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.

Author Info:
By: Amit Dey
Rating: 5 stars5 stars5 stars5 stars5 stars / 65
February 17, 2002
  1. · Building An Outlook COM Add-In With VC /ATL
  2. · Getting started
  3. · Building a minimal COM add-in
  4. · Command and conquer
  5. · Command and conquer (contd.)
  6. · Lord of the (disp) sinks
  7. · Conclusion

print this article

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:

#import "C:\Program Files\Microsoft Office\Office\mso9.dll" rename_namespace("Office") named_guids

using namespace Office;

#import "C:\Program Files\Microsoft Office\Office\MSOUTL9.olb" rename_namespace("Outlook"), raw_interfaces_only, named_guids

using namespace Outlook;

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:

STDMETHODIMP CAddin::OnConnection(IDispatch * Application, ext_ConnectMode ConnectMode,IDispatch * AddInInst, SAFEARRAY * * custom)


CComPtr < Office::_CommandBars> spCmdBars;

CComPtr < Office::CommandBar> spCmdBar;

// QI() for _Application

CComQIPtr spApp(Application);


// get the CommandBars interface that represents Outlook's

//toolbars & menu items

CComPtr spExplorer;


HRESULT hr = spExplorer->get_CommandBars(&spCmdBars);


return hr;


// now we add a new toolband to Outlook

// to which we'll add 2 buttons

CComVariant vName("OutlookAddin");

CComPtr spNewCmdBar;

// position it below all toolbands

//MsoBarPosition::msoBarTop = 1

CComVariant vPos(1);

CComVariant vTemp(VARIANT_TRUE); // menu is temporary


//Add a new toolband through Add method

// vMenuTemp holds an unspecified parameter

//spNewCmdBar points to the newly created toolband

spNewCmdBar = spCmdBars->Add(vName, vPos, vEmpty, vTemp);

//now get the toolband's CommandBarControls

CComPtr < Office::CommandBarControls> spBarControls;

spBarControls = spNewCmdBar->GetControls();


//MsoControlType::msoControlButton = 1

CComVariant vToolBarType(1);

//show the toolbar?

CComVariant vShow(VARIANT_TRUE);

CComPtr < Office::CommandBarControl> spNewBar;

CComPtr < Office::CommandBarControl> spNewBar2;

// Add first button

spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);


// Add 2nd button

spNewBar2 = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);


_bstr_t bstrNewCaption(OLESTR("Item1"));

_bstr_t bstrTipText(OLESTR("Tooltip for Item1"));

// get CommandBarButton interface for each toolbar button

// so we can specify button styles and stuff

// each button displays a bitmap and caption next to it

CComQIPtr < Office::_CommandBarButton> spCmdButton(spNewBar);

CComQIPtr < Office::_CommandBarButton> spCmdButton2(spNewBar2);



// to set a bitmap to a button, load a 32x32 bitmap

// and copy it to clipboard. Call CommandBarButton's PasteFace()

// to copy the bitmap to the button face. to use

// Outlook's set of predefined bitmap, set button's FaceId to //the

// button whose bitmap you want to use

HBITMAP hBmp =(HBITMAP)::LoadImage(_Module.GetResourceInstance(),


// put bitmap into Clipboard



::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);



// set style before setting bitmap


HRESULT hr = spCmdButton->PasteFace();

if (FAILED(hr))

return hr;




spCmdButton->PutTooltipText(OLESTR("Tooltip for Item1"));

spCmdButton->PutTag(OLESTR("Tag for Item1"));

//show the toolband



//specify predefined bitmap





spCmdButton2->PutTooltipText(OLESTR("Tooltip for Item2"));

spCmdButton2->PutTag(OLESTR("Tag for Item2"));




//code to add new menubar to be added here

//read on


blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials