ATLEventHandling Sample: Handles Word Events Using IDispEventImpl and IDispEventSimpleImpl

The ATLEventHandling sample demonstrates the use of IDispEventImpl and IDispEventSimpleImpl to handle events fired by Microsoft Word.

The sample consists of an ATL project that includes two COM classes: CSimple and CNotSoSimple. These classes represent COM objects that display a message box in response to document change events fired by Microsoft Word. CSimple shows how to handle events using ATL's IDispEventSimpleImpl class template, and CNotSoSimple shows how to handle events using ATL's IDispEventImpl class template. The code in the two classes is identical except for the differences caused by the use of the different ATL templates.

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample


This sample requires that Microsoft Office 2000 be installed. If you want to use this sample with other versions of Microsoft Office, you must specify the path to the mso dll file referenced in the StdAfx.h file under the ATLEventHandling directory. For example, Office 2000 makes use of mso9.dll and Office XP makes use of mso.dll.

To build and run this sample

  1. Open the solution file ATLEventHandling.sln.

  2. If you are using Excel 2000, modify the following line in StdAfx.h:


    to instead be:

    #define OFFICE_VER OFFICE2000
  3. From the Build menu, click Build Solution.

  4. Run MFCClient.exe.

You'll see a dialog box with a drop-down list allowing you to run the test using IDispEventImpl or IDispEventSimpleImpl. Select one of these items, and then click the Start button. If Microsoft Word is installed on your computer, a new instance will be loaded and the test component will start to receive events sent by Word whenever the active document changes.

You can trigger events by creating new documents, loading documents, or switching to a different document using Word's Window menu. Each time the document changes, a message box will be displayed indicating which component is receiving the events and the name of the newly active document. You can disconnect the event handler from the instance of Word by using the Stop button on the MFCClient dialog box, by changing the selection of the drop-down list, or by closing Word.

Close the sample application using the OK or Cancel command button or the dialog box's Close button.


This sample uses the following keywords:

_ATL_FUNC_INFO, BEGIN_SINK_MAP, END_SINK_MAP, SINK_ENTRY_EX, SINK_ENTRY_INFO, IDispEventImpl, IDispEventSimpleImpl, IDispEventSimpleImpl::DispEventAdvise, IDispEventSimpleImpl::DispEventUnadvise

See Also

Other Resources

ATL Samples