Working Programmatically with Media Player in PowerPoint
Getting Started with Media in PowerPoint: Learn how to work with various video operations by using Media Player in Microsoft PowerPoint 2010.
Applies to: Office 2010 | PowerPoint 2010 | VBA
In this article
Add a Sample Video File to a Presentation
Add a Standard Module to a PowerPoint Presentation
Add the Code to the Visual Basic Editor
Run the Code
Next Steps
Published: April 2011
Provided by: Frank Rice, Microsoft Corporation
Windows Media Player provides an intuitive, easy-to-use interface to play and organize digital media files. You can also create CDs of your favorite music, transfer music from CDs, and perform many other tasks. In this topic, you work programmatically with some of the different options that are available for Media Player in Microsoft PowerPoint 2010. To complete this task, you must do the following:
Add a Sample Video File to a Presentation
Add a Standard Module to a PowerPoint Presentation
Add the Code to the Visual Basic Editor
Run the Code
Add a Sample Video File to a Presentation
In this task, you insert a sample video file into a PowerPoint 2010 presentation.
To insert a video file
Download the Microsoft Office PowerPoint 2003 sample video.
Start PowerPoint.
On the Insert menu, click Video, and then click Video from File.
Navigate to the sample video file, and then click Insert.
After you add the video, play it to make sure that there are no problems.
Add a Standard Module to a PowerPoint Presentation
In this task, you open a PowerPoint 2010 presentation, open the Visual Basic Editor, and then insert a standard module.
To add a standard module to a PowerPoint presentation
Start PowerPoint.
On the Developer tab, click Visual Basic to open the Visual Basic Editor.
Note
If you do not see the Developer tab in PowerPoint, click the File tab, and then click Options. In the categories pane, click Popular, select Show Developer tab in the Ribbon, and then click OK.
On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.
Add the Code to the Visual Basic Editor
In this task, you add code that sets up different bookmarks in the video and contains modules that use different methods of the Player class.
To add code to the Visual Basic Editor
In the Projects pane, click Module1.
Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.
Private shapeID As Integer Private plyr As Player Sub TestPlayer() Dim shp As Shape For Each shp In ActivePresentation.Slides(1).Shapes ' Find the first media shape on the slide: If shp.Type = msoMedia Then shapeID = shp.id Exit For End If Next shp ' Add some bookmarks to the media. Delete all existing bookmarks ' first, so you can execute this code multiple times. Dim i As Integer With shp.MediaFormat For i = .MediaBookmarks.Count To 1 Step -1 .MediaBookmarks(i).Delete Next i End With With shp.MediaFormat.MediaBookmarks .Add 1000, "Bookmark 1" .Add 5000, "Bookmark 2" .Add 8000, "Bookmark 3" End With ' Store away a reference to the Player instance. Set plyr = Application.Windows(1).View.Player(shapeID) End Sub Sub Play() plyr.Play End Sub Sub Pause() plyr.Pause End Sub Sub GotoNextBookmark() plyr.GotoNextBookmark End Sub Sub GotoPreviousBookmark() plyr.GotoPreviousBookmark End Sub Sub GotoPosition() plyr.CurrentPosition = 4000 End Sub
Run the Code
In this task, you run the VBA code that sets up the video. You then run the different methods of the Player class and watch the results in Media Player.
To run the setup code
- In the Visual Basic Editor, place the cursor in TestPlayer, and then press F5 to run the procedure. This adds bookmarks to the video.
In the following steps, you run each module to see its effects on Media Player. The best way to see the code in action is to place the Visual Basic Editor window next to the PowerPoint window.
To step through the code
Drag the Visual Basic Editor window to the right side of your monitor.
Drag the PowerPoint window to the left side of the monitor and adjust both windows until you can see them both.
Click the Visual Basic Editor window, place the cursor in a module, and then press the F5 key to run the code. Watch how the code affects the video. Repeat this step for each module.