DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 824021 - Last Review: October 6, 2011 - Revision: 3.0

On This Page

INTRODUCTION

This article describes how to handle Microsoft Office PowerPoint events from an Automation client that is developed by using Microsoft Visual Basic .NET 2003.

Construct an event handler

You can use either of the following methods to construct an event handler by using Visual Basic .NET. The method that you choose depends on how you want to associate the event handler with an event.
  • Method 1

    Typically, you create an event handler by using the Handles keyword with the WithEvents keyword. When you declare an object reference variable by using the WithEvents keyword, Visual Basic .NET automatically connects to the events of the object at run time. To handle a specific event for an object, add the relevant handler by using the Class list and the Method list of the Visual Studio .NET environment while you are in Code view.
  • Method 2

    Visual Basic .NET provides another way to handle events. You can use the AddHandler statement and the RemoveHandler statement so that you can dynamically start and dynamically stop event handling for a specific event.

    Note If you use the WithEvents keyword to construct the event handlers, you may receive a System.Reflection.TargetInvocationException exception. Therefore, use the AddHandler event to construct the event handlers.

Create the Visual Basic .NET Automation client that handles PowerPoint events

The following sets of steps show how to use the AddHandler statement to handle the PowerPoint application events from an Automation client that is developed by using Visual Basic .NET.

To create a Visual Basic .NET project, follow these steps:
  1. Start Microsoft Visual Studio .NET 2003.
  2. On the File menu, click New, and then click Project.
  3. In the Project Types list, click Visual Basic Projects. In the Templates list, click Windows Application.
  4. Name the project PowerPointEvents, and then click OK.

    By default, a form that is named Form1 is created.
To add references to the Microsoft PowerPoint Object Library and to the Microsoft Graph Object Library, follow these steps:
  1. On the Project menu, click Add Reference.
  2. On the COM tab, click one of the following, and then click Select:
    • For Microsoft Office PowerPoint 2007, click Microsoft PowerPoint 12.0 Object Library.
    • For Microsoft Office PowerPoint 2003, click Microsoft PowerPoint 11.0 Object Library.
  3. On the COM tab, click one of the following, and then click Select:
    • For Microsoft Graph 12.0, click Microsoft Graph 12.0 Object Library.
    • For Microsoft Graph 11.0, click Microsoft Graph 11.0 Object Library.
  4. In the Add References dialog box, click OK to accept your selections.
  5. Double-click the form.

    The Code window opens.
  6. At the top of the Form1.vb file, add the following code :
    	Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
    	Imports Office = Microsoft.Office.Core
    	Imports Graph = Microsoft.Office.Interop.Graph
    
  7. On the View menu, click Designer.
  8. On the View menu, click Toolbox, and then add a button to Form1.
  9. Double-click Button1.

    The Code window opens at the Button1_Click event.
  10. In the Code window, put the following code before the Button1_Click event:
    	Dim oApp As PowerPoint.Application
    	Dim oPres As PowerPoint.Presentation
    
  11. Add the following code to the Button1_Click event:
    	Const sTemplate = "C:\Program Files\Microsoft Office\Templates\Presentation Designs\Orbit.pot"
            ' If the previous template is not available, change the template name to a template name that is on your system.
            Const sVideo = "C:\WINDOWS\system32\oobe\images\intro.wmv"
    
            Dim oPresentations As PowerPoint.Presentations
            Dim oSlides As PowerPoint.Slides
            Dim oSlide As PowerPoint.Slide
            Dim oShapes As PowerPoint.Shapes
            Dim oShape As PowerPoint.Shape
            Dim oMovie As PowerPoint.Shape
            Dim oAnimationSettings As PowerPoint.AnimationSettings
            Dim oPlaySettings As PowerPoint.PlaySettings
            Dim oTextFrame As PowerPoint.TextFrame
            Dim oTextRange As PowerPoint.TextRange
            Dim oFont As PowerPoint.Font
            Dim oOLEFormat As PowerPoint.OLEFormat
            Dim oShadow As PowerPoint.ShadowFormat
            Dim oForeColor As PowerPoint.ColorFormat
            Dim oRange As PowerPoint.SlideRange
            Dim oSlideShowTransition As PowerPoint.SlideShowTransition
    
            'Start PowerPoint and then make the PowerPoint window visible but minimized.
            oApp = New PowerPoint.Application
    
            'Add event handlers.
            AddHandler oApp.SlideShowBegin, AddressOf oApp_SlideShowBegin
            AddHandler oApp.SlideShowNextSlide, AddressOf oApp_SlideShowNextSlide
            AddHandler oApp.PresentationClose, AddressOf oApp_PresentationClose
    
            oApp.Visible = True
            oApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized
    
            'Create a new presentation that is based on the specified template.
            oPresentations = oApp.Presentations
            oPres = oPresentations.Open(sTemplate, , , True)
    
            'Build slide 1.
            'Add text to the slide, change the font, and then insert or position a 
            'movie on the first slide.
            oSlides = oPres.Slides
            oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
            oShapes = oSlide.Shapes
            oShape = oShapes.Item(1)
            oTextFrame = oShape.TextFrame
            oTextRange = oTextFrame.TextRange
            oTextRange.Text = "My sample presentation"
            oFont = oTextRange.Font
            oFont.Name = "Comic Sans MS"
            oFont.Size = 48
    
            oMovie = oShapes.AddMediaObject(sVideo, 150, 150, 500, 350)
            oAnimationSettings = oMovie.AnimationSettings
            oPlaySettings = oAnimationSettings.PlaySettings
            oPlaySettings.PlayOnEntry = True
            oPlaySettings.HideWhileNotPlaying = True
    
            'NAR to release references.
            NAR(oPlaySettings)
            NAR(oAnimationSettings)
            NAR(oMovie)
            NAR(oFont)
            NAR(oTextRange)
            NAR(oTextFrame)
            NAR(oShape)
            NAR(oShapes)
            NAR(oSlide)
            NAR(oSlides)
    
            'Build slide 2.
            'Add text to the slide title and then format the text. Also, add a chart to the
            'slide and then change the chart type to a 3D Column Clustered.
            oSlides = oPres.Slides
            oSlide = oSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
            oShapes = oSlide.Shapes
            oShape = oShapes.Item(1)
            oTextFrame = oShape.TextFrame
            oTextRange = oTextFrame.TextRange
            oTextRange.Text = "My chart"
            oFont = oTextRange.Font
            oFont.Name = "Comic Sans MS"
            oFont.Size = 48
    
            Dim oChart As Graph.Chart
            oShape = oShapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8")
            oOLEFormat = oShape.OLEFormat
            oChart = oOLEFormat.Object
            oChart.ChartType = Graph.XlChartType.xl3DColumnClustered
    
            'NAR to release references.
            NAR(oChart)
            NAR(oOLEFormat)
            NAR(oFont)
            NAR(oTextRange)
            NAR(oTextFrame)
            NAR(oShape)
            NAR(oShapes)
            NAR(oSlide)
            NAR(oSlides)
    
    
            'Build slide 3.
            'Add a text effect to the slide and then apply shadows to the text effect.
            oSlides = oPres.Slides
            oSlide = oSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank)
            oSlide.FollowMasterBackground = False
            oShapes = oSlide.Shapes
            oShape = oShapes.AddTextEffect(Office.MsoPresetTextEffect.msoTextEffect27, _
                "The End", "Impact", 96, False, False, 230, 200)
    
            oShadow = oShape.Shadow
            oForeColor = oShadow.ForeColor
            oForeColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppForeground
            oShadow.Visible = True
            oShadow.OffsetX = 3
            oShadow.OffsetY = 3
    
            'NAR to release references.
            NAR(oShadow)
            NAR(oForeColor)
            NAR(oShape)
            NAR(oShapes)
            NAR(oSlide)
            NAR(oSlides)
    
    
            'Modify the slide show transition settings for all three slides in
            'the presentation.
            Dim SlideIdx(3) As Integer
            SlideIdx(0) = 1
            SlideIdx(1) = 2
            SlideIdx(2) = 3
    
            oSlides = oPres.Slides
            oRange = oSlides.Range(SlideIdx)
            oSlideShowTransition = oRange.SlideShowTransition
            oSlideShowTransition.AdvanceOnTime = False
            oSlideShowTransition.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut
    
    
            'Run the slide show in a separate method.
            RunSlideShow()
    
            'NAR to release references.
            NAR(oSlideShowTransition)
            NAR(oRange)
            NAR(oSlides)
    
            'Close the presentation without saving changes and then quit PowerPoint.
            oPres.Saved = True
            oPres.Close()
    
            'NAR the presentation objects.
            NAR(oPres)
            NAR(oPresentations)
    
            'Remove all event handlers.
            RemoveHandler oApp.SlideShowBegin, AddressOf oApp_SlideShowBegin
            RemoveHandler oApp.SlideShowNextSlide, AddressOf oApp_SlideShowNextSlide
            RemoveHandler oApp.PresentationClose, AddressOf oApp_PresentationClose
    
            'Quit PowerPoint.
            oApp.Quit()
            NAR(oApp)
    
            GC.Collect()
    
  12. In the Code window, put the following code below the Button1_Click event:
    	Private Sub RunSlideShow()
                Dim oSettings As PowerPoint.SlideShowSettings
    	    Dim oSlideShowWindows As PowerPoint.SlideShowWindows
    
    	    oSettings = oPres.SlideShowSettings
    	    oSettings.StartingSlide = 1
           	    oSettings.EndingSlide = 3
    
                'Run the slide show. Wait for the slide show to finish.
                oSettings.Run()
    
                oSlideShowWindows = oApp.SlideShowWindows
    
                On Error Resume Next
                Do While oSlideShowWindows.Count >= 1
    	        System.Windows.Forms.Application.DoEvents()
           	    Loop
    
    	    NAR(oSlideShowWindows)
                NAR(oSettings)
    	End Sub
    
        	'NAR function to remove reference.
       	Private Sub NAR(ByVal o As Object)
                Try
    	        System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
                Catch
                Finally
                    o = Nothing
                End Try
        	End Sub
    
  13. Add the following event handlers below the previous code:
    	'Event Handlers
    
        	Private Sub oApp_SlideShowBegin(ByVal Wn As Microsoft.Office.Interop.PowerPoint.SlideShowWindow)
                'Change the position and the size of the slide show window.
                Dim oView As PowerPoint.SlideShowView
    
                With Wn
                    .Height = 325
                    .Width = 400
                    .Left = 100
                    .Activate()
                End With
    
               'Start the movie at the first slide.
                oView = Wn.View
                oView.Next()
    
                NAR(oView)
                NAR(Wn)
        	End Sub
    
        	Private Sub oApp_SlideShowNextSlide(ByVal Wn As Microsoft.Office.Interop.PowerPoint.SlideShowWindow)
                'Change the color and the type of pointer depending on the slide.
                Dim Showpos As Integer
                Dim oView As PowerPoint.SlideShowView
                Dim oColorFormat As PowerPoint.ColorFormat
    
                oView = Wn.View
                Showpos = oView.CurrentShowPosition + 1
    
                If Showpos = 3 Then
                    oColorFormat = oView.PointerColor
                    oColorFormat.RGB = RGB(255, 0, 0)
                    oView.PointerType = PowerPoint.PpSlideShowPointerType.ppSlideShowPointerPen
                Else
                    oColorFormat = oView.PointerColor
                    oColorFormat.RGB = RGB(0, 0, 0)
                    oView.PointerType = PowerPoint.PpSlideShowPointerType.ppSlideShowPointerArrow
                End If
    
                NAR(oColorFormat)
                NAR(oView)
                NAR(Wn)
        	End Sub
    
        	Private Sub oApp_PresentationClose(ByVal Pres As Microsoft.Office.Interop.PowerPoint.Presentation)
                'Before you close the presentation, save the presentation as HTML.
                Pres.SaveAs("C:\TestEvents.htm", PowerPoint.PpSaveAsFileType.ppSaveAsHTML)
                NAR(Pres)
        	End Sub
    

Try it out

  1. Press F5 to run the application.
  2. Click Button1.

    PowerPoint starts the slide show. Notice the following:
    • The first slide starts a movie.
    • The pointer type changes.
    • The color changes on the second slide that has a chart.
  3. Close the presentation.

REFERENCES

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
308330  (http://support.microsoft.com/kb/308330/ ) How to handle PowerPoint 2002 events by using Visual Basic .NET 2002
254009  (http://support.microsoft.com/kb/254009/ ) PowerPoint 2000 event demonstration available for download
303717  (http://support.microsoft.com/kb/303717/ ) How to use automation to create and to show a PowerPoint 2002 presentation or an Office PowerPoint 2003 presentation by using Visual Basic .NET 2002 or Visual Basic .NET 2003
823996  (http://support.microsoft.com/kb/823996/ ) PIA is not found when you reference an Office Type Library in Visual Studio .NET

APPLIES TO
  • Microsoft Office PowerPoint 2007
  • Microsoft Office PowerPoint 2003
  • Microsoft Visual Basic .NET 2003 Standard Edition
Keywords: 
kbexpertiseinter kbhowtomaster kbautomation KB824021
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support