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();
ATLASSERT(spCmdCtrls);
// we want to add a menu entry to Outlook's 6th(Tools) menu //item
CComVariant vItem(5);
spCmdCtrl= spCmdCtrls->GetItem(vItem);
ATLASSERT(spCmdCtrl);
IDispatchPtr spDisp;
spDisp = spCmdCtrl->GetControl();
// a CommandBarPopup interface is the actual menu item
CComQIPtr < Office::CommandBarPopup> ppCmdPopup(spDisp);
ATLASSERT(ppCmdPopup);
spCmdBarCtrls = ppCmdPopup->GetControls();
ATLASSERT(spCmdBarCtrls);
CComVariant vMenuType(1); // type of control - menu
CComVariant vMenuPos(6);
CComVariant vMenuEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
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);
ATLASSERT(spNewMenu);
spNewMenu->PutCaption(bstrNewMenuText);
spNewMenu->PutEnabled(VARIANT_TRUE);
spNewMenu->PutVisible(VARIANT_TRUE);
//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);
ATLASSERT(spCmdMenuButton);
spCmdMenuButton->PutStyle(Office::msoButtonIconAndCaption);
// 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().
spCmdMenuButton->PasteFace();
// show the menu
spNewMenu->PutVisible(VARIANT_TRUE);
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.


Next: Lord of the (disp) sinks >>
More C++ Articles
More By Amit Dey