Microsoft KB Archive/824021

= How to handle PowerPoint events by using Visual Basic .NET 2003 =

Article ID: 824021

Article Last Modified on 5/24/2007

-

APPLIES TO


 * Microsoft Office PowerPoint 2007
 * Microsoft Office PowerPoint 2003
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



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:  On the Project menu, click Add Reference. 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.  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.</li></ul> </li> In the Add References dialog box, click OK to accept your selections.</li> Double-click the form.

The Code window opens.</li>  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 </li> On the View menu, click Designer.</li> On the View menu, click Toolbox, and then add a button to Form1.</li> Double-click Button1.

The Code window opens at the Button1_Click event.</li>  In the Code window, put the following code before the Button1_Click event: Dim oApp As PowerPoint.Application Dim oPres As PowerPoint.Presentation </li>  Add the following code to the Button1_Click event: Const sTemplate = &quot;C:\Program Files\Microsoft Office\Templates\Presentation Designs\Orbit.pot&quot; ' If the previous template is not available, change the template name to a template name that is on your system. Const sVideo = &quot;C:\WINDOWS\system32\oobe\images\intro.wmv&quot;

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 = &quot;My sample presentation&quot; oFont = oTextRange.Font oFont.Name = &quot;Comic Sans MS&quot; 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 = &quot;My chart&quot; oFont = oTextRange.Font oFont.Name = &quot;Comic Sans MS&quot; oFont.Size = 48

Dim oChart As Graph.Chart oShape = oShapes.AddOLEObject(150, 150, 480, 320, &quot;MSGraph.Chart.8&quot;) 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, _           &quot;The End&quot;, &quot;Impact&quot;, 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 </li>  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 </li>  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(&quot;C:\TestEvents.htm&quot;, PowerPoint.PpSaveAsFileType.ppSaveAsHTML) NAR(Pres) End Sub </li></ol>

Try it out

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

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

<div class="references_section">