Share via


Working with Embedded Media in PowerPoint 2010

Office Visual How To

Summary:  Learn how to programmatically embed media files into a Microsoft PowerPoint 2010 presentation.

Applies to: Office 2010 | PowerPoint 2010 | Visual Studio

Published:  October 2010

Provided by:  Michael Case, iSoftStone

Overview

By using Microsoft PowerPoint 2010, you can effectively engage your audience by embedding and customizing videos within your presentation. This Visual How To shows how to use the PowerPoint 2010 Primary Interop Assembly (PIA) to programmatically create a new PowerPoint 2010 presentation, add a new slide to the presentation, embed a video into the new slide, and format how the video will be displayed.

Code It

This Visual How To uses the following steps to show how to programmatically embed media files in a PowerPoint presentation:

  1. Copy the sample video file to the C:\TEMP directory.

  2. Create a Windows console application solution in Microsoft Visual Studio 2010.

  3. Add references to the required assemblies.

  4. Add the sample code to the solution.

Copying the Sample Video File to the C:\temp Directory

The sample code presented with this Visual How To uses the Butterfly.wmv file that is usually located in the sample videos directory. The Butterfly.wmv file has to be copied to the C:\TEMP directory for the sample code to work without modification.

If the Butterfly.wmv file is not available, you can use another video file. When you use another video file, you must modify the sample code's mediaFileName variable to include the full path and name of the video file to be embedded.

PowerPoint 2010 allows the following video file formats to be embedded: Windows Media Video (.wmv), Windows Video (.avi), Windows Media (.asf), Movie (.mpg or .mpeg), and Adobe Flash Media (.flv).

To copy the sample video file to the C:\TEMP directory

  1. Start Windows Explorer and navigate to the folder that contains the Butterfly.wmv file. This file is usually located in the C:\Users\Current User\Videos directory.

  2. Copy the Butterfly.wmv file to the C:\TEMP directory.

Creating a Windows Console Application in Visual Studio 2010

This Visual How To uses a Windows console application as the framework for the sample code. The console application type was selected only for its simplicity. Other application types could use the same approach presented here.

To create a Windows console application in Visual Studio 2010

  1. Start Visual Studio 2010.

  2. On the File menu, point to New, and then click Project.

  3. In the New Project dialog box, select the Visual C# Windows type in the left pane.

  4. Select Console Application in the middle pane.

  5. Name the project and solution PowerPoint2010EmbeddedVideo.

  6. Click OK to create the solution.

Adding References to the Required Assemblies

This Visual How To uses the PowerPoint 2010 Primary Interop Assembly (PIA) to create and manipulate a PowerPoint presentation. The PowerPoint 2010 PIA exposes the PowerPoint 2010 object model to managed code and is installed with Visual Studio 2010.

The PowerPoint 2010 PIA has a dependency to classes in the Microsoft.Office.Core namespace that requires a reference to the Office assembly to be added, also.

To add a reference to the PowerPoint 2010 Primary Interop Assembly

  1. On the Visual Studio Project menu, click Add Reference.

  2. On the .NET tab of the Add Reference dialog box, select version 14.0.0.0 of the Microsoft.Office.Interop.PowerPoint component, and then click OK to add the reference.

To add a reference to the Office assembly

  1. On the Visual Studio Project menu, click Add Reference.

  2. On the .NET tab of the Add Reference dialog box, select version 14.0.0.0 of the Office component, and then click OK to add the reference.

Adding the Sample Code to the Solution

Replace the contents of the Program.cs source file with the following code. The code creates a PowerPoint 2010 presentation named PowerPoint2010EmbeddedVideo.pptx in the C:\TEMP folder. The code then adds a new slide to the presentation, embeds a video file in the slide, and then formats how the video will be displayed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;

namespace PowerPoint2010EmbeddedVideo
{
  class Program
  {
    static void Main(string[] args)
    {
        // Declare variables to hold references to the PowerPoint objects.
        Application application = new Application();
        Presentation presentation;
        Master master;
        CustomLayout cl;
        Slide slide;
        Microsoft.Office.Interop.PowerPoint.Shape mediaFile;

        // Declare a variable for the new presentation file.
        String presentationName = @"c:\temp\PowerPoint2010EmbeddedVideo";

        // This variable references the video file that will be embedded
        // into the presentation.
        // It may need to be updated to reference a path and file name to 
        // a video file located on your computer.
        String mediaFileName = @"C:\temp\Butterfly.wmv";

        try
        {
            // Create a new PowerPoint 2010 presentation.
            presentation = 
              application.Presentations.Add(MsoTriState.msoFalse);

            // Add a new slide to the presentation.
            master = presentation.SlideMaster;
            cl = master.CustomLayouts[PpSlideLayout.ppLayoutChartAndText];
            slide = presentation.Slides.AddSlide(1, cl);

            // Add a title above the video.
            slide.Shapes[1].TextFrame.TextRange.Text = "Butterfly";

            // Embed the video file into the slide.
            mediaFile = slide.Shapes.AddMediaObject2(
                FileName: mediaFileName,
                LinkToFile: MsoTriState.msoFalse,
                SaveWithDocument: MsoTriState.msoCTrue,
                Left: 90f,
                Top: 114f);

            // Resize the video to its original size.
            mediaFile.ScaleHeight(1f, MsoTriState.msoCTrue);
            mediaFile.ScaleWidth(1f, MsoTriState.msoCTrue);

            // Format to have a beveled edge.
            mediaFile.ThreeD.BevelTopDepth = 8;
            mediaFile.ThreeD.BevelTopInset = 6;
            mediaFile.ThreeD.BevelBottomType = 
                MsoBevelType.msoBevelSoftRound;

            // Format to display in a rounded rectangle shape.
            mediaFile.AutoShapeType = 
                MsoAutoShapeType.msoShapeRoundedRectangle;

            // Save the presentation.
            presentation.SaveAs(
                presentationName, 
                PpSaveAsFileType.ppSaveAsOpenXMLPresentation, 
                MsoTriState.msoFalse);
        }
        finally
        {
            // Release the references to the PowerPoint objects.
            mediaFile = null;
            slide = null;
            presentation = null;

            // Release the ApplicationClass object.
            if (application != null)
            {
                application.Quit();
                application = null;
            }

                  GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
  }
}
Read It

The following excerpt from the preceding code example shows how to use the PowerPoint 2010 PIA to embed a video into a slide and format how it will be displayed. The code embeds the video by using the AddMediaObject2 method. The FileName parameter is the name of the video file to embed. The LinkToFile parameter is set to false, and the SaveWithDocument parameter is set to true to embed the file within the presentation. The Left and Top parameters are used to define the location of the upper-left corner for the video display area. Named arguments are used because all of the parameters are not passed to the AddMediaObject2 method. For more information about named arguments, see Named and Optional Arguments.

The video file is then reset to its original size by calling the ScaleHeight and ScaleWidth methods. The first parameter is the factor to use when resizing, and the second parameter defines that resizing should be based on the original size.

The video file is then formatted to be displayed with a beveled edge by using the ThreeDBevelTopDepth, BevelTopInset, and BevelBottomType properties. The video file is also formatted to display in a rounded rectangle shape by setting the AutoShapeType property to the msoAutoShapeType.msoShapeRoundedRectangle value.

// Embed the video file into the slide.
mediaFile = slide.Shapes.AddMediaObject2(
    FileName: mediaFileName,
    LinkToFile: MsoTriState.msoFalse,
    SaveWithDocument: MsoTriState.msoCTrue,
    Left: 90f,
    Top: 114f);

// Resize the video to its original size.
mediaFile.ScaleHeight(1f, MsoTriState.msoCTrue);
mediaFile.ScaleWidth(1f, MsoTriState.msoCTrue);

// Format to have a beveled edge.
mediaFile.ThreeD.BevelTopDepth = 8;
mediaFile.ThreeD.BevelTopInset = 6;
mediaFile.ThreeD.BevelBottomType = 
    MsoBevelType.msoBevelSoftRound;

// Format to display in a rounded rectangle shape.
mediaFile.AutoShapeType = 
    MsoAutoShapeType.msoShapeRoundedRectangle;
See It

Watch video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/02e51c2e-388c-4b8c-9479-e506351b0e7c]

Length: 04:38

Click to grab code

Grab the Code

Explore It