Programmatically Add Media to a PowerPoint Presentation

Office 2010

Office Quick Note banner

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

In this article
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

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

  1. Start PowerPoint.

  2. On the Developer tab, click Visual Basic to open the Visual Basic Editor.

    Note 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.

  3. 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

  1. In the Projects pane, click Module1.

  2. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Important note Important

    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.