Note Microsoft Visual C++ .NET (2002) supports both the managed code
model that is provided by the Microsoft .NET Framework and the unmanaged native
Microsoft Windows code model. The information in this article applies only to
unmanaged Visual C++ code.
This article describes how to handle Microsoft PowerPoint 2000 events or PowerPoint 2002 events by using Microsoft Visual C++ .NET 2002 or Visual C++ .NET 2003 and Microsoft Foundation Classes.
PowerPoint fires events in response to user actions or in
response to some methods that are called through Automation. The Application object in the PowerPoint object model fires these events on its
outgoing interface, EApplication.
To view this interface and its methods, you can use
the OLE/COM Object Viewer, as follows:
On the Tools menu in Visual Studio .NET, select OLE/COM Object Viewer.
Expand the node for Type Libraries and select Microsoft PowerPoint Object Library in the list.
On the Object menu, select View to open the library in the ITypeLib Viewer.
Expand the node for coclass Application and select EApplication.
Note that EApplication is derived from IDispatch and is not the dispinterface that is commonly used as a source
interface. If the source interface is a dispinterface, you can determine the
dispatch identifiers (DISPIDs) for its methods by using the OLE/COM Object
Viewer. However, because EApplication is not a dispinterface, you cannot determine the DISPIDs for
PowerPoint events by examining the type library.
The following table
lists DISPIDs for the events that the PowerPoint 2000 and PowerPoint 2002
object models expose:
You can sink a PowerPoint event
in your C++ application by calling IConnectionPointContainer::FindConnectionPoint to find the connection point for the desired event interface, and
then IConnectionPoint::Advise with the IUnknown interface of your implementation for that event.
Create a C++ Application to Handle PowerPoint Events
Create a new dialog box-based application by using the
Microsoft Foundation Classes (MFC) Application Wizard in Visual C++ .NET. Name
your project PPTEventsDemo, and then accept the default settings. The dialog
box is created by default, along with the corresponding Ppteventsdemodlg.cpp
and Ppteventsdemodlg.h files.
Add three buttons to your dialog box, and then name the
buttons Launch PowerPoint, Establish Connection Point and Register Sink, and UnRegister Sink and Do Clean Up, respectively. Add a list box to the dialog box. The list box
displays the names of the events as they occur.
In the Project explorer window in Class View, right-click PPTEventsDemo, point to Add, and then click Add Class. In the Add Class dialog box, select MFC class from TypeLibrary and then click Open. This starts the Add Class from TypeLib wizard.
Select Microsoft PowerPoint 10.0 object library (for PowerPoint 2002) or Microsoft PowerPoint 9.0 object library (for PowerPoint 2000) from the Available TypeLibraries drop-down list. The Interfaces list box displays all of the interfaces that the type library
exposes. Select _Application and then click the > button. Accept the defaults and then click Finish. This generates the CApplication wrapper class, which is derived from COleDispatchDriver. The implementation and definition of this class is available in
the Capplication.h file.
In Visual Studio .NET, click Resource View on the View menu to show the PPTEventsDemo dialog box. Double-click Launch PowerPoint to show the Code View window for Ppteventsdemodlg.cpp, where an
empty event handler has been inserted for the Click event of that button. Add the following code to the handler for
the Launch PowerPoint button:
Add the following code to the handler for the Establish Connection Point and Register Sink button:
///*********************** Start of code to get connection point **************
// Declare the events that you want to catch.
// Look for the coclass for Application in the Msppt9.olb typelib,
// then look for the word "source." The EApplication interface
// is the next search target. When you find it, you will see the
// following GUID for the event interface.
static const GUID IID_IEApplication =
// Steps for setting up events.
// 1. Get the IConnectionPointContainer interface of the server.
// 2. Call IConnectionPointContainer::FindConnectionPoint()
// to find the event that you want to catch.
// 3. Call IConnectionPoint::Advise() with the IUnknown
// interface of your implementation of the events.
// Get the (PPT) IConnectionPointContainer interface of the server.
hr = pptapp.m_lpDispatch->QueryInterface(
if(FAILED(hr)) AfxMessageBox("Couldn't get IConnectionPointContainer interface.");
// Find a connection point for the events that you are interested in.
hr = pConnPtContainer->FindConnectionPoint(
if(FAILED(hr)) AfxMessageBox("Couldn't find connection point via event GUID.");
//Instantiate the sink object.
m_sink = new CMyPPTEventsHandler();
//Update the list box when you obtain the events in the event handler.
m_sink->m_pListBox = m_listBox;
// Get the IUnknown interface of your event implementation.
LPUNKNOWN pUnk = NULL;
pUnk = m_sink->GetInterface(&IID_IUnknown);
// Setup advisory connection.
hr = m_pConnectionPoint->Advise(pUnk, &m_sink->cookie);
// Release the IConnectionPointContainer interface.
// *********************** End of code to get connection point ******************
Add the following code to a handler for the UnRegister Sink and Do Clean Up button:
//Use the cookie to unregister the sink.
//Detach the application object from the server.
Add the following code to the bottom of the constructor in
the CPPTEventsDemoDlg class:
m_pConnectionPoint = NULL;
pptapp = NULL;
Before you return from CPPTEventsDemoDlg::OnInitDialog, add the following line of code:
//Get the MFC class pointer for the list box on the dialog box.
m_listBox = (CListBox*) GetDlgItem(IDC_LIST1);
Be sure that you place the following #include statements at the beginning of the Ppteventsdemodlg.cpp file:
In Class View in Project Explorer, right-click PPTEventsDemo, point to Add, and then click Add Class. In the AddClass dialog box, select MFC class under Templates, and then click Open. Type CMyPPTEventsHandler for the class
name, select CCmdTarget for the base class, and then select Automation. For other fields, accept the defaults. Click Finish. This creates a new CMyPPTEventsHandler MFC class that is derived from CCmdTarget. This class is defined in the Myppteventshandler.h file and is
implemented in Myppteventshandler.cpp. This is the Event Handler class that contains methods that are called in response to the
This creates a new In the Myppteventshandler.h file,
forward declare the following class:
This creates a new In the Myppteventshandler.h file, add
the following members to the public declarations in the CMyPPTEventsHandler class:
This creates a new Add the following methods to the
protected declarations in the CMyPPTEventsHandler class:
This is the GUID for the EApplication outgoing event interface for the PowerPoint Application class. Also, note that the dispatch map for this class maps the
DISPIDs of the events to the event handler methods.
This creates a new In the Ppteventsdemo.cpp file, add the
following #include statement
before the following:
This creates a new Add the following lines of code at the
top of the InitInstance method of the Ppteventsdemoapp.cpp file:
AfxMessageBox("Unable to initialize COM");
Test the Application
Press F5 to build and run the program. The dialog box
Click Launch PowerPoint. PowerPoint starts and becomes visible.
Click Establish Connection Point and Register Sink to set up the event sinks.
Create a new presentation in PowerPoint. The WindowActivate, NewPresentation, PresentationNewSlide, SlideSelectionChange, and WindowSelectionChange events fire.
Start the slide show and play it through the end. The SlideShowBegin, SlideShowNextSlide, and SlideShowEnd events fire.
Save the presentation. The PresentationBeforeSave and PresentationSave events fire.
Close the presentation. The PresentationClose event fires, and the events that were triggered by PowerPoint
2002 and handled by the program appear in the list box.
NOTE: These are the events that PowerPoint 2002 fires. You may not see
some of these events in PowerPoint 2000.
Click UnRegister Sink and Do Clean Up to disconnect the event sinks.