Article ID: 308825 - Last Review: January 30, 2007 - Revision: 5.2
How to handle PowerPoint events with Visual C# .NET This article was previously published under Q308825
For a Microsoft Visual Basic .NET version of this article, see
308330
(http://support.microsoft.com/kb/308330/
)
.
For a Microsoft Visual C++
.NET version of this article, see
309309
(http://support.microsoft.com/kb/309309/
)
.
This article demonstrates how to automate PowerPoint and
handle events using Visual C# .NET.
With Microsoft Visual Studio .NET, you cannot use delegates
to sink events with Microsoft PowerPoint 2002. PowerPoint uses the
IDispatch interface to fire events. To correctly sink the events, the
Visual C# .NET application must use the
IConnectionPointContainer and
IConnectionPoint interfaces. The receiving application must also know the DISPIDs
of the events to sink. These DISPIDs are not listed in PowerPoint's type
library, but are listed in the sample code below for reference.
Create the Sample C# Automation Client Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project . Under Project types click Visual C# Projects , then click Windows Application under Templates . Form1 is created by default. Add a reference to the Microsoft PowerPoint Object Library . To do this, follow these steps:
On the Project menu, click Add Reference . On the COM tab, locate Microsoft Powerpoint 10.0 Object
Library , and then click Select .NOTE : If you have not already done so, it is recommended that you
download, and then install the Microsoft Office XP Primary Interop Assemblies
(PIAs).
For more information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
328912
(http://support.microsoft.com/kb/328912/
)
INFO: Microsoft Office XP PIAs Are
Available for Download
Click OK in the Add References dialog box to accept your selection. On the View menu, click Toolbox to display the Toolbox and add two buttons and a list box to
Form1. In sequence, double-click Button1 , Button2 , and Form1 . In the code window, replace the following code
private void button1_Click(object sender, System.EventArgs e)
{
}
private void button2_Click(object sender, System.EventArgs e)
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
with:
private UCOMIConnectionPoint m_oConnectionPoint;
private int m_Cookie;
private PowerPoint.ApplicationClass oPPT;
private void button1_Click(object sender, System.EventArgs e)
{
// QI for IConnectionPointContainer.
UCOMIConnectionPointContainer oConnPointContainer = (UCOMIConnectionPointContainer) oPPT;
// Get the GUID of the EApplication interface.
Guid guid=typeof(PowerPoint.EApplication).GUID;
// Find the connection point.
oConnPointContainer.FindConnectionPoint(ref guid,out m_oConnectionPoint);
// Call Advise to sink up the connection.
m_oConnectionPoint.Advise(this,out m_Cookie);
}
[DispId(2001)]
public void WindowSelectionChange(PowerPoint.Selection Sel)
{
this.listBox1.Items.Add("WindowSelectionChange");
}
[DispId(2002)]
public void WindowBeforeRightClick(PowerPoint.Selection Sel,bool Cancel)
{
this.listBox1.Items.Add("WindowBeforeRightClick");
}
[DispId(2003)]
public void WindowBeforeDoubleClick(PowerPoint.Selection Sel,bool Cancel)
{
this.listBox1.Items.Add("WindowBeforeDoubleClick");
}
[DispId(2004)]
public void PresentationClose(PowerPoint.Presentation Pres)
{
this.listBox1.Items.Add("PresentationClose");
}
[DispId(2005)]
public void PresentationSave(PowerPoint.Presentation Pres)
{
this.listBox1.Items.Add("PresentationSave");
}
[DispId(2006)]
public void PresentationOpen(PowerPoint.Presentation Pres)
{
this.listBox1.Items.Add("PresentationOpen");
}
[DispId(2007)]
public void NewPresentation(PowerPoint.Presentation Pres)
{
this.listBox1.Items.Add("NewPresentation");
}
[DispId(2008)]
public void PresentationNewSlide(PowerPoint.Slide Sld)
{
this.listBox1.Items.Add("PresentationNewSlide");
}
[DispId(2009)]
public void WindowActivate(PowerPoint.Presentation Pres,PowerPoint.DocumentWindow Wn)
{
this.listBox1.Items.Add("WindowActivate");
}
[DispId(2010)]
public void WindowDeactivate(PowerPoint.Presentation Pres,PowerPoint.DocumentWindow Wn)
{
this.listBox1.Items.Add("WindowDeactivate");
}
[DispId(2011)]
public void SlideShowBegin(PowerPoint.SlideShowWindow Wn)
{
this.listBox1.Items.Add("SlideShowBegin");
}
[DispId(2012)]
public void SlideShowNextBuild(PowerPoint.SlideShowWindow Wn)
{
this.listBox1.Items.Add("SlideShowNextBuild");
}
[DispId(2013)]
public void SlideShowNextSlide(PowerPoint.SlideShowWindow Wn)
{
this.listBox1.Items.Add("SlideShowNextSlide");
}
[DispId(2014)]
public void SlideShowEnd(PowerPoint.Presentation Pres)
{
this.listBox1.Items.Add("SlideShowEnd");
}
[DispId(2015)]
public void PresentationPrint(PowerPoint.Presentation Pres)
{
this.listBox1.Items.Add("PresentationPrint");
}
[DispId(2016)]
public void SlideSelectionChanged(PowerPoint.SlideRange SldRange)
{
this.listBox1.Items.Add("SlideSelectionChanged");
}
[DispId(2017)]
public void ColorSchemeChanged(PowerPoint.SlideRange SldRange)
{
this.listBox1.Items.Add("ColorSchemeChanged");
}
[DispId(2018)]
public void PresentationBeforeSave(PowerPoint.Presentation Pres,bool Cancel)
{
this.listBox1.Items.Add("PresentationBeforeSave");
}
[DispId(2019)]
public void SlideShowNextClick(PowerPoint.SlideShowWindow Wn,PowerPoint.Effect nEffect)
{
this.listBox1.Items.Add("SlideShowNextClick");
}
private void button2_Click(object sender, System.EventArgs e)
{
m_oConnectionPoint.Unadvise(m_Cookie);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oPPT);
GC.Collect();
}
private void Form1_Load(object sender, System.EventArgs e)
{
//Create an instance of PowerPoint.
oPPT = new PowerPoint.ApplicationClass();
// Show PowerPoint to the user.
oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
}
Add the following to the using section at the top of the code window:
using System.Runtime.InteropServices;
Test the program. To do this, follow these steps:
Press F5 to build and run the program. PowerPoint is
started. Click Button1 to set up the event sinks. Create a new presentation in PowerPoint. The
WindowActivate , NewPresentation , PresentationNewSlide and WindowSelectionChange events fire. Save the presentation. The PresentationSave event fires. Close the presentation. The PresentationClose event fires. Activate Form1 in the program. The events that were triggered by PowerPoint and
handled by the program appear in the list box. Click Button2 to disconnect the event sinks. Close Form1 .
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
254009
(http://support.microsoft.com/kb/254009/
)
INFO:
PowerPoint 2000 Event Demonstration Available for Download
For more information on Office Automation, see the
following Microsoft Office Development support site:
APPLIES TO Microsoft Visual C# .NET 2002 Standard Edition Microsoft PowerPoint 2002 Standard Edition kbautomation kbconnpts kbhowto KB308825
Provide feedback on this information
Did this information solve your problem?
Was this information relevant?
What can we do to improve this information?
To protect your privacy, do not include contact information in your feedback.
Thank you! Your feedback is used to help us improve our support content. For more assistance options, please visit the
Help and Support Home Page .