Microsoft Knowledge Base Email Alertz

KBAlertz.com: (308330) - This article demonstrates how to automate PowerPoint and handle events using Visual Basic .NET.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 308330 - Last Review: January 30, 2007 - Revision: 4.2

How to handle PowerPoint 2002 events by using Visual Basic .NET 2002

This article was previously published under Q308330
For a Microsoft Visual C# .NET version of this article, see 308825  (http://kbalertz.com/Feedback.aspx?kbNumber=308825/EN-US/ ) .
For a Microsoft Visual C++ .NET version of this article, see 309309  (http://kbalertz.com/Feedback.aspx?kbNumber=309309/EN-US/ ) .

SUMMARY

This article describes how to handle Microsoft PowerPoint 2002 events by using Microsoft Visual Basic .NET 2002.

MORE INFORMATION

With Microsoft Visual Studio .NET, you cannot use delegates to sink events with PowerPoint. PowerPoint uses the IDispatch interface to fire events. To correctly sink the events, the Visual Basic .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 Visual Basic .NET Automation Client

  1. Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual Basic Projects, then click Windows Application under Templates. Form1 is created by default.
  2. Add a reference to the Microsoft PowerPoint Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. 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 additional information about Office XP PIAs, click the article number below to view the article in the Microsoft Knowledge Base:
      328912  (http://kbalertz.com/Feedback.aspx?kbNumber=328912/EN-US/ ) INFO: Microsoft Office XP PIAs Are Available for Download
    3. Click OK in the Add References dialog box to accept your selection. If you receive a prompt to generate wrappers for the libraries you selected, click Yes.
  3. On the View menu, click Toolbox to display the Toolbox and add two buttons and a list box to Form1.
  4. In sequence, double-click Button1, Button2, and Form1.
  5. In the code window, replace the following code
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                                                    Handles Button1.Click
    
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                                                    Handles Button2.Click
    
    End Sub
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                                                   Handles MyBase.Load
    
    End Sub
    					
    with:
        Private m_oConnectionPoint As UCOMIConnectionPoint
        Private m_Cookie As Integer
        Private oPPT As PowerPoint.Application
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
                                                                       Handles MyBase.Load
            ' Create a new instance of PowerPoint.
            oPPT = New PowerPoint.Application()
    
            ' Show PowerPoint to the user.
            oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                                                        Handles Button2.Click
            ' Call Unadvise to remove the sink to the connection.
            m_oConnectionPoint.Unadvise(m_Cookie)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oPPT)
            GC.Collect()
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                                                        Handles Button1.Click
            Dim oConnPointContainer As UCOMIConnectionPointContainer
            ' QI for IConnectionPointContainer.
            oConnPointContainer = CType(oPPT, UCOMIConnectionPointContainer)
            ' Create a new variable that contains the GUID of the 
            ' PowerPoint.EApplication interface.
            Dim guid As New Guid("914934C2-5A91-11CF-8700-00AA0060263B")
            ' Find the connection point.
            oConnPointContainer.FindConnectionPoint(guid, m_oConnectionPoint)
            ' Call Advise to sink up the connection.
            m_oConnectionPoint.Advise(Me, m_Cookie)
        End Sub
    
        <DispId(2001)> Public Sub WindowSelectionChange(ByVal Sel As PowerPoint.Selection)
            Me.ListBox1.Items.Add("WindowSelectionChange")
        End Sub
    
        <DispId(2002)> Public Sub WindowBeforeRightClick(ByVal Sel As PowerPoint.Selection,
                                                                   ByVal Cancel As Boolean)
            Me.ListBox1.Items.Add("WindowBeforeRightClick")
        End Sub
    
        <DispId(2003)> Public Sub WindowBeforeDoubleClick(ByVal Sel As PowerPoint.Selection,
                                                                    ByVal Cancel As Boolean)
            Me.ListBox1.Items.Add("WindowBeforeDoubleClick")
        End Sub
    
        <DispId(2004)> Public Sub PresentationClose(ByVal Pres As PowerPoint.Presentation)
            Me.ListBox1.Items.Add("PresentationClose")
        End Sub
    
        <DispId(2005)> Public Sub PresentationSave(ByVal Pres As PowerPoint.Presentation)
            Me.ListBox1.Items.Add("PresentationSave")
        End Sub
    
        <DispId(2006)> Public Sub PresentationOpen(ByVal Pres As PowerPoint.Presentation)
            Me.ListBox1.Items.Add("PresentationOpen")
        End Sub
    
        <DispId(2007)> Public Sub NewPresentation(ByVal Pres As PowerPoint.Presentation)
            Me.ListBox1.Items.Add("NewPresentation")
        End Sub
    
        <DispId(2008)> Public Sub PresentationNewSlide(ByVal Sld As PowerPoint.Slide)
            Me.ListBox1.Items.Add("PresentationNewSlide")
        End Sub
    
        <DispId(2009)> Public Sub WindowActivate(ByVal Pres As PowerPoint.Presentation,
                                                 ByVal Wn As PowerPoint.DocumentWindow)
            Me.ListBox1.Items.Add("WindowActivate")
        End Sub
    
        <DispId(2010)> Public Sub WindowDeactivate(ByVal Pres As PowerPoint.Presentation,
                                                   ByVal Wn As PowerPoint.DocumentWindow)
            Me.ListBox1.Items.Add("WindowDeactivate")
        End Sub
    
        <DispId(2011)> Public Sub SlideShowBegin(ByVal Wn As PowerPoint.SlideShowWindow)
            Me.ListBox1.Items.Add("SlideShowBegin")
        End Sub
    
        <DispId(2012)> Public Sub SlideShowNextBuild(ByVal Wn As PowerPoint.SlideShowWindow)
            Me.ListBox1.Items.Add("SlideShowNextBuild")
        End Sub
    
        <DispId(2013)> Public Sub SlideShowNextSlide(ByVal Wn As PowerPoint.SlideShowWindow)
            Me.ListBox1.Items.Add("SlideShowNextSlide")
        End Sub
    
        <DispId(2014)> Public Sub SlideShowEnd(ByVal Pres As PowerPoint.Presentation)
            Me.ListBox1.Items.Add("SlideShowEnd")
        End Sub
    
        <DispId(2015)> Public Sub PresentationPrint(ByVal Pres As PowerPoint.Presentation)
            Me.ListBox1.Items.Add("PresentationPrint")
        End Sub
    
    
        <DispId(2016)> Public Sub SlideSelectionChanged(ByVal SldRange As PowerPoint.SlideRange)
            Me.ListBox1.Items.Add("SlideSelectionChanged")
        End Sub
    
        <DispId(2017)> Public Sub ColorSchemeChanged(ByVal SldRange As PowerPoint.SlideRange)
            Me.ListBox1.Items.Add("ColorSchemeChanged")
        End Sub
    
        <DispId(2018)> Public Sub PresentationBeforeSave(ByVal Pres As PowerPoint.Presentation,
                                                                       ByVal Cancel As Boolean)
            Me.ListBox1.Items.Add("PresentationBeforeSave")
        End Sub
    
        <DispId(2019)> Public Sub SlideShowNextClick(ByVal Wn As PowerPoint.SlideShowWindow,
                                                         ByVal nEffect As PowerPoint.Effect)
            Me.ListBox1.Items.Add("SlideShowNextClick")
        End Sub
    					
  6. Add the following to the Imports section at the top of the code window:
    Imports System.Runtime.InteropServices 
    Imports Microsoft.Office.Interop
    					
  7. Test the program. To do this, follow these steps:
    1. Press F5 to build and run the program. PowerPoint is started.
    2. Click Button1 to set up the event sinks.
    3. Create a new presentation in PowerPoint.

      The WindowActivate, NewPresentation, PresentationNewSlide and WindowSelectionChange events fire.
    4. Save the presentation.

      The PresentationSave event fires.
    5. Close the presentation.

      The PresentationClose event fires.
    6. Activate Form1 in the program. The events that were triggered by PowerPoint and handled by the program appear in the list box.
    7. Click Button2 to disconnect the event sinks.
    8. Close Form1.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
254009  (http://kbalertz.com/Feedback.aspx?kbNumber=254009/EN-US/ ) INFO: PowerPoint 2000 Event Demonstration Available for Download
For more information on Office Automation, see the following Microsoft Office Development support site:
FAQs and Highlights for Office Development
http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)

APPLIES TO
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbautomation kbhowto KB308330
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Anonymous User Report As Irrelevant  
Written: 6/7/2005 11:04 PM
Display with Sample Application Code

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please