Home arrow C++ arrow Page 5 - 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 (contd.)
(Page 5 of 7 )

The ActiveMenuBar property of the CommandBars collection returns a CommandBar object that represents the active menu bar in the container application (i.e. Outlook 2000 for our example). Next, we query for the active menubar's controls collection (CommandBarControls) through the GetControls method. Since we want to add a popup menu item to Outlook's tools (6th position) menu, we retrieve the 6th item in the ActiveMenuBars control collection and call its add method to create a new menu item which is attached to the tools menu.

Here's the code snippet to accomplish everything we have just discussed:


//code to add toolbar here


_bstr_t bstrNewMenuText(OLESTR("New Menu Item"));

CComPtr < Office::CommandBarControls> spCmdCtrls;

CComPtr < Office::CommandBarControls> spCmdBarCtrls;

CComPtr < Office::CommandBarPopup> spCmdPopup;

CComPtr < Office::CommandBarControl> spCmdCtrl;

// get CommandBar that is Outlook's main menu

hr = spCmdBars->get_ActiveMenuBar(&spCmdBar);

if (FAILED(hr))

return hr;

// get menu as CommandBarControls

spCmdCtrls = spCmdBar->GetControls();


// we want to add a menu entry to Outlook's 6th(Tools) menu //item

CComVariant vItem(5);

spCmdCtrl= spCmdCtrls->GetItem(vItem);


IDispatchPtr spDisp;

spDisp = spCmdCtrl->GetControl();

// a CommandBarPopup interface is the actual menu item

CComQIPtr < Office::CommandBarPopup> ppCmdPopup(spDisp);


spCmdBarCtrls = ppCmdPopup->GetControls();


CComVariant vMenuType(1); // type of control - menu

CComVariant vMenuPos(6);


CComVariant vMenuShow(VARIANT_TRUE); // menu should be visible

CComVariant vMenuTemp(VARIANT_TRUE); // menu is temporary

CComPtr < Office::CommandBarControl> spNewMenu;

// now create the actual menu item and add it

spNewMenu = spCmdBarCtrls->Add(vMenuType, vMenuEmpty, vMenuEmpty, vMenuEmpty, vMenuTemp);





//we'd like our new menu item to look cool and display

// an icon. Get menu item as a CommandBarButton

CComQIPtr < Office::_CommandBarButton> spCmdMenuButton(spNewMenu);



// we want to use the same toolbar bitmap for menuitem too.

// we grab the CommandBarButton interface so we can add

// a bitmap to it through PasteFace().


// show the menu


return S_OK;


With that under our belt, it's F5 time. If everything has gone OK, the project builds successfully and you are about to get a first glimpse of our add-in in action. Since we are going to run outlook to test our add-in, in the "Executable for Debug" dialog, browse to the current path for the outlook executable (outlook.exe). In outlook, go to the Tools->Options menu, and under the other tab, click advanced options. From the advanced options dialog, click on the COM add-ins button. Next, check the entry for our add-in in the add-ins available list box and click OK. As our add-in is loaded by outlook, a new docked toolband with two buttons is created. Take a look at the cool looking menu item that we've just added to outlook's Tools menu.

Our tool bar items in action

Our popup menu items in action
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