Programmatically Add Media to a PowerPoint Presentation
Getting Started with Media in PowerPoint: Learn how to programmatically add various media to a Microsoft PowerPoint 2010 presentation.
Last modified: May 10, 2011
Applies to: Office 2010 | PowerPoint 2010 | VBA
Published: April 2011
Provided by: Frank Rice, Microsoft Corporation
Adding high-quality videos and audio to a slide presentation can greatly increase the impact that it has on your viewers. In this topic, you programmatically add a video and audio file to a Microsoft PowerPoint 2010 slide. To complete this task, you must do the following:
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
On the Developer tab, click Visual Basic to open the Visual Basic Editor.
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.
In this task, you add programming code that demonstrates how to embed and link a video and audio file into a PowerPoint slide.
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.
Make sure that you change the two constants in the code to point to your own video and audio files.
Const videoFileName As String = "C:\Users\Public\Videos\Sample Videos\Bee.wmv" Const audioFileName As String = "C:\Users\Public\Music\Sample Music\Happy Birthday.mp3" Sub AddMedia() With ActivePresentation.Slides(1).Shapes Dim shp As Shape ' Set only one of height and width--PowerPoint will maintain the correct ' aspect ratio for the video. This video is linked (as opposed to embedded). Set shp = .AddMediaObject2(videoFileName, msoTrue, msoFalse, 10, 10, 320) DisplayMediaInfo shp ' This audio is embedded, not linked. Set shp = .AddMediaObject2(audioFileName, msoFalse, msoTrue, 350, 10) DisplayMediaInfo shp End With End Sub Private Sub DisplayMediaInfo(shp As Shape) If shp.Type = msoMedia Then Debug.Print "Embedded: " & shp.MediaFormat.IsEmbedded Debug.Print "Linked: " & shp.MediaFormat.IsLinked End If End Sub
In this task, you run the VBA code that embeds the audio file and links to the video file. The code also writes status information to the Immediate window.
To run the code
In the Visual Basic Editor, place the cursor in the AddMedia procedure and then press F5 to run the code. You should see the video file and a link to the audio file in Slide 1. Note the values in the Immediate window that specify the embedded and linked status of the media.