Microsoft KB Archive/309309

= How to handle PowerPoint 2000 events or PowerPoint 2002 events by using Visual C++ .NET 2002 or Visual C++ .NET 2003 and Microsoft Foundation Classes =

Article ID: 309309

Article Last Modified on 8/23/2005

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft PowerPoint 2000 Standard Edition
 * Microsoft PowerPoint 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Foundation Class Library 4.2

-



This article was previously published under Q309309





For a Microsoft Visual C# .NET version of this article, see 308825.

For a Microsoft Visual Basic .NET version of this article, see 308330.

IN THIS TASK
SUMMARY
 * PowerPoint Events
 * Create a C++ Application to Handle PowerPoint Events
 * Test the Application

REFERENCES



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.



SUMMARY
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.

back to the top

PowerPoint Events
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:
 * 1) On the Tools menu in Visual Studio .NET, select OLE/COM Object Viewer.
 * 2) Expand the node for Type Libraries and select Microsoft PowerPoint Object Library in the list.
 * 3) On the Object menu, select View to open the library in the ITypeLib Viewer.
 * 4) 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.

back to the top

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: if(!pptapp.CreateDispatch(&quot;Powerpoint.Application&quot;)) {        AfxMessageBox(&quot;Could not create Powerpoint object.&quot;); return; }

pptapp.put_Visible((long) 1);

  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 &quot;source.&quot; The EApplication interface // is the next search target. When you find it, you will see the // following GUID for the event interface. // 914934C2-5A91-11CF-8700-00AA0060263B static const GUID IID_IEApplication = {0x914934C2,0x5A91,0x11CF, {0x87,0x00,0x00,0xAA,0x00,0x60,0x26,0x3b}};

// 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.

HRESULT hr;

// Get the (PPT) IConnectionPointContainer interface of the server. IConnectionPointContainer *pConnPtContainer; hr = pptapp.m_lpDispatch->QueryInterface(        IID_IConnectionPointContainer,         (void **)&pConnPtContainer      ); if(FAILED(hr)) AfxMessageBox(&quot;Couldn't get IConnectionPointContainer interface.&quot;); ASSERT(!FAILED(hr));

// Find a connection point for the events that you are interested in. hr = pConnPtContainer->FindConnectionPoint(        IID_IEApplication,         &m_pConnectionPoint      ); if(FAILED(hr)) AfxMessageBox(&quot;Couldn't find connection point via event GUID.&quot;); ASSERT(!FAILED(hr));

//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); ASSERT(pUnk);

// Setup advisory connection. hr = m_pConnectionPoint->Advise(pUnk, &m_sink->cookie); ASSERT(!FAILED(hr));

// Release the IConnectionPointContainer interface. pConnPtContainer->Release; // *********************** End of code to get connection point ******************

</li>  Add the following code to a handler for the UnRegister Sink and Do Clean Up button: //Use the cookie to unregister the sink. m_pConnectionPoint->Unadvise(m_sink->cookie); m_pConnectionPoint->Release; //Detach the application object from the server. pptapp.DetachDispatch;

</li>  Add the following code to the bottom of the constructor in the CPPTEventsDemoDlg class: m_pConnectionPoint = NULL; pptapp = NULL; </li>  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); </li>  Be sure that you place the following #include statements at the beginning of the Ppteventsdemodlg.cpp file: </li>  Forward declare the following classes in the Ppteventsdemodlg.h file: class CMyPPTEventsHandler; class CApplication; </li>  Add the following declarations as private members to CPPTEventsDemoDlg: IConnectionPoint* m_pConnectionPoint; CApplication pptapp; CMyPPTEventsHandler* m_sink; CListBox* m_listBox; </li> 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 PowerPoint events.
 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;CApplication.h&quot;
 * 3) include &quot;MyPPTEventsHandler.h&quot;
 * 4) include &quot;PPTEventsDemo.h&quot;
 * 5) include &quot;PPTEventsDemoDlg.h&quot;

</li>  This creates a new In the Myppteventshandler.h file, forward declare the following class: class CPresentation; </li>  This creates a new In the Myppteventshandler.h file, add the following members to the public declarations in the CMyPPTEventsHandler class: DWORD cookie; CListBox* m_pListBox; </li>  This creates a new Add the following methods to the protected declarations in the CMyPPTEventsHandler class: void WindowSelectionChange(LPDISPATCH Pres); void WindowBeforeRightClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel); void WindowBeforeDoubleClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel); void PresentationClose ( LPDISPATCH Pres); void PresentationSave( LPDISPATCH Pres); void PresentationOpen( LPDISPATCH Pres); void NewPresentation( LPDISPATCH Pres); void PresentationNewSlide( LPDISPATCH Pres); void WindowActivate( LPDISPATCH Pres,LPDISPATCH Wn); void WindowDeactivate(LPDISPATCH Pres, LPDISPATCH Wn); void SlideShowBegin(LPDISPATCH Wn); void SlideShowNextBuild( LPDISPATCH Wn); void SlideShowNextSlide( LPDISPATCH Wn); void SlideShowEnd( LPDISPATCH Pres); void PresentationPrint(LPDISPATCH Pres); void SlideSelectionChanged(LPDISPATCH SldRange); void ColorSchemeChanged(LPDISPATCH SldRange); void PresentationBeforeSave(LPDISPATCH Pres, VARIANT_BOOL * Cancel); void SlideShowNextClick(LPDISPATCH Wn, LPDISPATCH nEffect); void PresentationPrint(LPDISPATCH Pres);

These are the PowerPoint event handlers.

NOTE: The following 4 event handlers are not available in PowerPoint 2000, and are not called if the client has PowerPoint 2000: void SlideSelectionChanged(LPDISPATCH SldRange); void ColorSchemeChanged(LPDISPATCH SldRange); void PresentationBeforeSave(LPDISPATCH Pres, VARIANT_BOOL * Cancel); void SlideShowNextClick(LPDISPATCH Wn, LPDISPATCH nEffect); </li>  This creates a new Replace the entire contents of Myppteventshandler.cpp with the following: // MyPPTEventsHandler.cpp : implementation file. //


 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;PPTEventsDemo.h&quot;
 * 3) include &quot;MyPPTEventsHandler.h&quot;

// CMyPPTEventsHandler.

IMPLEMENT_DYNAMIC(CMyPPTEventsHandler, CCmdTarget) CMyPPTEventsHandler::CMyPPTEventsHandler {   EnableAutomation; }

CMyPPTEventsHandler::~CMyPPTEventsHandler { }

void CMyPPTEventsHandler::OnFinalRelease {   // When the last reference for an Automation object is released, // OnFinalRelease is called. The base class automatically // deletes the object. Add additional cleanup required for your // object before you call the base class.

CCmdTarget::OnFinalRelease; }

BEGIN_MESSAGE_MAP(CMyPPTEventsHandler, CCmdTarget) END_MESSAGE_MAP

BEGIN_DISPATCH_MAP(CMyPPTEventsHandler, CCmdTarget) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;WindowSelectionChange&quot;,2001,WindowSelectionChange,VT_EMPTY, VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler, &quot;WindowBeforeRightClick&quot;, 2002 ,WindowBeforeRightClick, VT_EMPTY , VTS_DISPATCH VTS_BOOL) DISP_FUNCTION_ID(CMyPPTEventsHandler, &quot;WindowBeforeDoubleClick&quot;, 2003 ,WindowBeforeDoubleClick, VT_EMPTY, VTS_DISPATCH VTS_BOOL) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;PresentationClose&quot;,2004,PresentationClose,VT_EMPTY, VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;PresentationSave&quot;,2005,PresentationSave,VT_EMPTY, VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;PresentationOpen&quot;,2006,PresentationOpen,VT_EMPTY, VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;NewPresentation&quot;,2007,NewPresentation,VT_EMPTY, VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;PresentationNewSlide&quot;,2008,PresentationNewSlide,VT_EMPTY, VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;WindowActivate&quot;,2009,WindowActivate,VT_EMPTY, VTS_DISPATCH VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;WindowDeactivate&quot;,2010,WindowDeactivate,VT_EMPTY, VTS_DISPATCH VTS_DISPATCH) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;SlideShowBegin&quot;,2011,SlideShowBegin,VT_EMPTY, VTS_DISPATCH ) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;SlideShowNextBuild&quot;,2012,SlideShowNextBuild,VT_EMPTY, VTS_DISPATCH ) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;SlideShowNextSlide&quot;,2013,SlideShowNextSlide,VT_EMPTY, VTS_DISPATCH ) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;SlideShowEnd&quot;,2014,SlideShowEnd,VT_EMPTY, VTS_DISPATCH ) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;PresentationPrint&quot;,2015,PresentationPrint,VT_EMPTY, VTS_DISPATCH ) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;SlideSelectionChanged&quot;,2016,SlideSelectionChanged,VT_EMPTY, VTS_DISPATCH ) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;ColorSchemeChanged&quot;,2017,ColorSchemeChanged,VT_EMPTY, VTS_DISPATCH ) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;PresentationBeforeSave&quot;,2018,PresentationBeforeSave,VT_EMPTY, VTS_DISPATCH VTS_BOOL) DISP_FUNCTION_ID(CMyPPTEventsHandler,&quot;SlideShowNextClick&quot;,2019,SlideShowNextClick,VT_EMPTY, VTS_DISPATCH VTS_DISPATCH ) END_DISPATCH_MAP

//The GUUID is different from the one originally generated by Class Wizard. This GUUID is the same as the one for the EApplication outgoing event interface. static const IID IID_IMyPPTEventsHandler = {0x914934C2,0x5A91,0x11CF, {0x87,0x00,0x00,0xAA,0x00,0x60,0x26,0x3b}};

BEGIN_INTERFACE_MAP(CMyPPTEventsHandler, CCmdTarget) INTERFACE_PART(CMyPPTEventsHandler, IID_IMyPPTEventsHandler, Dispatch) END_INTERFACE_MAP

// CMyPPTEventsHandler message handlers.

void CMyPPTEventsHandler::WindowSelectionChange(LPDISPATCH Pres) {   m_pListBox->AddString(&quot;WindowSelectionChange&quot;); return ; } void CMyPPTEventsHandler::WindowBeforeRightClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel) {   m_pListBox->AddString(&quot;WindowBeforeRightClick&quot;); return ; }

void CMyPPTEventsHandler::WindowBeforeDoubleClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel) {   m_pListBox->AddString(&quot;WindowBeforeDoubleClick&quot;); return; }

void CMyPPTEventsHandler::PresentationClose ( LPDISPATCH Pres) {   m_pListBox->AddString(&quot;PresentationClose&quot;); return ; }

void CMyPPTEventsHandler::PresentationSave( LPDISPATCH Pres) {   m_pListBox->AddString(&quot;PresentationSave&quot;); return ; }

void CMyPPTEventsHandler::PresentationOpen( LPDISPATCH Pres) {   m_pListBox->AddString(&quot;PresentationOpen&quot;); return ; } void CMyPPTEventsHandler::NewPresentation( LPDISPATCH Pres) {   m_pListBox->AddString(&quot;NewPresentation&quot;); return ; }

void CMyPPTEventsHandler::PresentationNewSlide( LPDISPATCH Pres) {   m_pListBox->AddString(&quot;PresentationNewSlide&quot;); return ; }

void CMyPPTEventsHandler::WindowActivate( LPDISPATCH Pres,LPDISPATCH Wn) {   m_pListBox->AddString(&quot;WindowActivate&quot;); return ; }

void CMyPPTEventsHandler::WindowDeactivate(LPDISPATCH Pres, LPDISPATCH Wn) {   m_pListBox->AddString(&quot;WindowDeactivate&quot;); return ; }

void CMyPPTEventsHandler::SlideShowBegin(LPDISPATCH Wn) {   m_pListBox->AddString(&quot;SlideShowBegin&quot;); return; }

void CMyPPTEventsHandler::SlideShowNextBuild( LPDISPATCH Wn) {   m_pListBox->AddString(&quot;SlideShowNextBuild&quot;); return ;

}

void CMyPPTEventsHandler::SlideShowNextSlide( LPDISPATCH Wn) {   m_pListBox->AddString(&quot;SlideShowNextSlide&quot;); return ; }

void CMyPPTEventsHandler::SlideShowEnd( LPDISPATCH Pres) {   m_pListBox->AddString(&quot;SlideShowEnd&quot;); return; }

void CMyPPTEventsHandler::PresentationPrint(LPDISPATCH Pres) {   m_pListBox->AddString(&quot;PresentationPrint&quot;); return; }

//The following events are not available for PowerPoint 2000. void CMyPPTEventsHandler::SlideSelectionChanged(LPDISPATCH SldRange) {   m_pListBox->AddString(&quot;SlideSelectionChanged&quot;); return ; }

void CMyPPTEventsHandler::ColorSchemeChanged(LPDISPATCH SldRange) {   m_pListBox->AddString(&quot;ColorSchemeChanged&quot;); return ; }

void CMyPPTEventsHandler::PresentationBeforeSave(LPDISPATCH Pres, VARIANT_BOOL * Cancel) {   m_pListBox->AddString(&quot;PresentationBeforeSave&quot;); return; }

void CMyPPTEventsHandler::SlideShowNextClick(LPDISPATCH Wn, LPDISPATCH nEffect) {   m_pListBox->AddString(&quot;SlideShowNextClick&quot;); return ; }

Note that the value of the IID_IMyPPTEventsHandler static constant is changed from the value that the Class Wizard originally generated. The value is changed to the following: static const IID IID_IMyPPTEventsHandler = {0x914934C2,0x5A91,0x11CF, {0x87,0x00,0x00,0xAA,0x00,0x60,0x26,0x3b}}; 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.

</li>  This creates a new In the Ppteventsdemo.cpp file, add the following #include statement before the following: </li>  This creates a new Add the following lines of code at the top of the InitInstance method of the Ppteventsdemoapp.cpp file: if(!AfxOleInit) {       AfxMessageBox(&quot;Unable to initialize COM&quot;); return FALSE; } </li></ol>
 * 1) include &quot;CApplication.h&quot;
 * 1) include &quot;PPTEventsDemoDlg.h&quot;

back to the top

Test the Application

 * 1) Press F5 to build and run the program. The dialog box appears.
 * 2) Click Launch PowerPoint. PowerPoint starts and becomes visible.
 * 3) Click Establish Connection Point and Register Sink to set up the event sinks.
 * 4) Create a new presentation in PowerPoint. The WindowActivate, NewPresentation, PresentationNewSlide, SlideSelectionChange, and WindowSelectionChange events fire.
 * 5) Start the slide show and play it through the end. The SlideShowBegin, SlideShowNextSlide, and SlideShowEnd events fire.
 * 6) Save the presentation. The PresentationBeforeSave and PresentationSave events fire.
 * 7) 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.


 * 1) Click UnRegister Sink and Do Clean Up to disconnect the event sinks.
 * 2) Close the dialog box.

back to the top

<div class="references_section">