Last Updated July 9, 2000 |
Install | Use | Source | Download | Changes | License | Contact

This is an ATL implementation of Microsoft's ComAddin sample. The original sample was presented in raw C++.

The project requires Microsoft Visual C++ 6, ATL 3, and Word 2000.

The project is an add-in component for Microsoft Word 2000. It creates a commandbar, a button on the bar, and a sink for the button's click event. It can be easily modified for other Office 2000 applications. The project demonstrates:


The add-in is registered during the build process. To run it on another machine, enter

regsvr32 AtlAddIn.dll
from the command line.


Start Microsoft Word 2000. You will be greeted by the add-in, and MyBar will appear as a floating command bar with My Button. You can dock the command bar by dragging it to the toolbar area of the Word interface.

You can hide the command bar by right-clicking it and unchecking MyBar on the context menu. You can then show it again by right-clicking in the toolbar area and checking MyBar.

Select Tools from the main menu and then Customize. Select 'COM Add-ins' from the Tools category. Drag the command to the Tools menu on Word's main menu bar. Now close the Customize dialog and select Tools from Word's main menu, and then 'Com Add-ins'. You can now 'disconnect' and 'connect' MyBar by unchecking and checking it. When the add-in is disconnected, MyBar can't be hidden and shown from the context menu.

Source contains a Microsoft Visual C++ 6.0 project. It is built with ATL 3.0, WTL 3.1, and the current Platform SDK.

Here is is the recipe for creating the project. Some of these procedures are matters of style rather than requirements.

  1. Create an ATL dll project.

  2. From the main menu, select Insert and then New ATL Object. Select Simple Object and name it 'MyAddin'.

  3. Click the ClassView tab. Right-click CMyAddin and select Implement Interface. Click <browse> and navigate to Msaddndr.tlb. Check _IDTExtensibility2.

  4. In Myaddin.h, add the no_implementation attribute to the import directive.

  5. Compile the project.

  6. Remove the type library. The project will work fine without this step. But all of the interfaces are defined by the system, so the type librar serves no purpose.
    1. Remove AtlAddIn.idl from the project.
    2. From the main menu, select View, and then Resource Includes, and remove the TYPELIB statement.
    3. In MyAddin.rgs, remove the TypeLib entry.
    4. In Atladdin.cpp, remove the TRUE argument of AtlRegisterServer and AtlUnRegisterServer.

  7. In MyAddin.h, remove IMyAddin as a base class of CMyAddin.

  8. In MyAddin.h, edit the interface map to include only a COM_INTERFACE_ENTRY for IDispatch and a COM_INTERFACE_ENTRY for _IDTExtensibility2.

  9. In MyAddin.h, add the member variables shown at the bottom of the CMyAddin definition, and initialize m_dwSinkCookie to zero in the constructor.

  10. Create two c++ files in the project, named BtnHandler.cpp and BtnHandler.h.

  11. In BtnHandler.h, add the following after the include directives (changing the dll path as appropriate for your system):
    #pragma warning(disable: 4146) 
    #import "C:\Program Files\Microsoft Office\Office\MSO9.DLL" \
    	raw_interfaces_only, raw_native_types, named_guids, \
    using namespace Office;

    The pragma disables the warning: "unary minus operator applied to unsigned type, result still unsigned".

    The import directive defines the MS Office classes and interfaces. Note that the no_namespace attribute is excluded from the import directive, and the using directive is included afterwards. This resolves a collision with the DocumentProperties definition in WinSpool.h.

  12. Define the CButtonHandler sink class in BtnHandler.h and implement it in BtnHandler.cpp.

  13. In MyAddin.h, #include BtnHandler.h.

  14. In MyAddin.cpp, Define the HR macro for error-checking. I learned this from Chris Sells, and I love it.
    #define HR(_ex) { HRESULT _hr = _ex; if( FAILED(_hr) ) return _hr; }
  15. Move the stub method implementations for _IDTExtensibility2 from MyAddin.h to MyAddin.cpp, leaving the declarations in the header file.

  16. In MyAddin.cpp, implement the _IDTExtensibility2 methods.

  17. In MyAddin.rgs, add the additional registry entries required for Office add-ins. You can change 'Word' in the script to any other Microsoft Office application.

Other than the sample itself, most of the Office add-in docs are written for Visual Basic.

SAMPLE: Comaddin.exe Office 2000 COM Add-In Written in Visual C++

Developing COM Add-Ins for Microsoft Office 2000

CommandBars Colection Object

CommandBar Object

CommandBarControls Collection Object

CommandBarControl Object

CommandBarButton Object

Download (21k).


July 9, 2000


Copyright© 2000 Tim Tabor. All rights reserved. No warrantees extended.


Please report bugs and feature requests to me.


Tim Tabor