Inserting Images into PowerPoint 2007 Presentations Using Visual Studio 2005 Tools for the Office System SE

Summary:   Insert an image programmatically into a Microsoft Office PowerPoint 2007 presentation by using Visual Studio 2005 Tools for the 2007 Microsoft Office System Second Edition.

Office Visual How To

Applies to:   2007 Microsoft Office System, Microsoft Office PowerPoint 2007, Microsoft Visual Studio 2005, Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System Second Edition

Ken Getz, MCW Technologies, LLC

October 2007

Overview

You might want to insert an image programmatically into the current slide in a Microsoft Office PowerPoint 2007 presentation. In this Office Visual How-to article, you learn how to use the PowerPoint 2007 object model to add your selected image to the current slide. You create an add-in for PowerPoint 2007 by using Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System Second Edition.

See It Inserting an Image into a PowerPoint Presentation

Watch the Video

Length: 6:34 | Size: 4.75 MB | Type: WMV file

Code It | Read It | Explore It

Code It

In this example, you create a Microsoft Office Fluent Ribbon customization that adds a button to the Insert tab in PowerPoint 2007. Clicking the button displays a dialog box so you can select an image. After you select the image, the add-in inserts the image and centers it on the current slide. To demonstrate the technique, follow these steps.

To insert a new button on the PowerPoint 2007 Insert tab

  1. In Visual Studio 2005, create a new Visual Studio 2005 Tools for Office Second Edition add-in for PowerPoint 2007. Name the new project InsertImageAddIn.

  2. In Solution Explorer, right-click the project, and then click Add New Item.

  3. In the Add New Item dialog box, select Ribbon support.

  4. Click Add to insert the new Office Fluent Ribbon customization.

  5. Modify the new Ribbon1.xml file, replacing the existing content with the following XML.

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
      <ribbon>
        <tabs>
          <tab idMso="TabInsert">
            <group id="MyGroup"
            label="Visual How To">
              <button id="button1"
                size="large"
                label="Insert image"
                screentip="Insert selected image."
                onAction="OnClick"
                imageMso="PictureInsertFromFilePowerPoint" />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    
  6. In Solution Explorer, right-click the InsertImageAddIn project, and then click Properties.

  7. In the Properties pane, click the Resources tab.

  8. In Solution Explorer, drag the Ribbon1.xml file into the Properties pane to add the Ribbon1.xml file as a project resource.

  9. Close the Properties pane, and click Yes when you are prompted to save.

  10. In the Ribbon1.vb or Ribbon1.cs file, uncomment the ThisAddIn partial class.

  11. In Microsoft Visual C# only, at the top of the code file, add the following using statement.

    using PowerPoint=Microsoft.Office.Interop.PowerPoint;
    
  12. In the Ribbon1 class, modify the existing GetCustomUI implementation to retrieve the Ribbon1 resource.

    In Visual C#, you must expand the IRibbonExtensibility members code region to find the procedure.

    Return My.Resources.Ribbon1
    
    return Properties.Resources.Ribbon1;
    
  13. In the Ribbon1 class, add the following callback procedure. (You click the new button on the Ribbon to call this procedure.)

    You can place the procedure in the Ribbon Callbacks code region, although there is no requirement where you place it, as long as it is in the Ribbon1 class.

    Public Sub OnClick(ByVal control As Office.IRibbonControl)
    End Sub
    
    public void OnClick(Office.IRibbonControl control)
    {
    }
    

Save the project, and run it. In PowerPoint 2007, on the Office Fluent Ribbon, click the Insert tab, and verify that you see the new button. Close PowerPoint 2007 and return to Visual Studio 2005.

To add a declaration to the Ribbon1 class

  1. In the Ribbon1 class, add the following declaration so you can refer to the PowerPoint 2007 application in the Ribbon1 class.

    Private Application As PowerPoint.Application = _
       Globals.ThisAddIn.Application
    
    private PowerPoint.Application Application =
       Globals.ThisAddIn.Application;
    
  2. In the Ribbon1 class, add the following procedure to create a new slide that uses the ppLayoutPictureWithCaption layout, and then insert the requested image in the image placeholder location.

    Public Sub AddPicture(ByVal pictureFileName As String)
      Try
        Dim presentation As PowerPoint.Presentation = _
          Application.ActivePresentation
    
        If presentation IsNot Nothing Then
          Dim slide As PowerPoint.Slide = _
           presentation.Slides.Add( _
           presentation.Slides.Count + 1, _
           PowerPoint.PpSlideLayout.ppLayoutPictureWithCaption)
    
          ' Shapes(2) is the image shape on this layout.
          Dim shape As PowerPoint.Shape = slide.Shapes(2)
    
          slide.Shapes.AddPicture(pictureFileName, _
           Microsoft.Office.Core.MsoTriState.msoFalse, _
           Microsoft.Office.Core.MsoTriState.msoTrue, _
           shape.Left, shape.Top, shape.Width, shape.Height)
    
          ' Insert the file name.
          slide.Shapes(1).TextFrame.TextRange.Text = pictureFileName
        End If
    
      Catch ex As Exception
        MessageBox.Show("Unable to insert selected picture: " & _
         ex.Message)
      End Try
    End Sub
    
    public void AddPicture(String pictureFileName)
    {
      try
      {
    
        PowerPoint.Presentation presentation =
          Application.ActivePresentation;
    
        if (presentation != null)
        {
          PowerPoint.Slide slide =
            presentation.Slides.Add(
            presentation.Slides.Count + 1,
            PowerPoint.PpSlideLayout.ppLayoutPictureWithCaption);
    
          // Shapes[2] is the image shape on this layout.
          PowerPoint.Shape shape = slide.Shapes[2];
    
          slide.Shapes.AddPicture(pictureFileName,
            Microsoft.Office.Core.MsoTriState.msoFalse,
            Microsoft.Office.Core.MsoTriState.msoTrue,
            shape.Left, shape.Top, shape.Width, shape.Height);
    
          // Insert the file name.
          slide.Shapes[1].TextFrame.TextRange.Text = pictureFileName;
          slide.Select();
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show("Unable to insert selected picture: " + 
          ex.Message);
      }
    }
    
  3. Replace the existing OnClick procedure with the following version.

    Add code to request a file name from the user, and call the AddPicture method with the name of the selected file.

    NoteNote

    In a real application, you might allow more file types in the File Open dialog box.

    Public Sub OnClick(ByVal control As Office.IRibbonControl)
      Dim dialog As New OpenFileDialog
      dialog.Filter = _
       "All pictures (*.jpg,*.jpeg,*.bmp,*.gif,*.tif)|" & _
       "*.jpf;*.jpeg;*.bmp;*.gif;*.tif|" & _
       "All files (*.*)|*.*"
      dialog.CheckFileExists = True
      If dialog.ShowDialog = DialogResult.OK Then
        AddPicture(dialog.FileName)
      End If
    End Sub
    
    public void OnClick(Office.IRibbonControl control)
    {
      OpenFileDialog dialog = new OpenFileDialog();
      dialog.Filter =
        "All pictures (*.jpg,*.jpeg,*.bmp,*.gif,*.tif)|" +
        "*.jpf;*.jpeg;*.bmp;*.gif;*.tif|" +
        "All files (*.*)|*.*";
    
      dialog.CheckFileExists = true;
      if (dialog.ShowDialog() == DialogResult.OK)
      {
        AddPicture(dialog.FileName);
      }
    }
    

Save and run the application. Click the Insert tab, and then click the new button.

Verify that you can insert an image, and that the new image appears on a new slide in the presentation.

Read It

It is not obvious how to calculate where to place an image on a slide. The simplest solution is to create a placeholder shape for the image, and then replace that shape with the image. You can look for other techniques, but this one seems to be effective.

As you might imagine, the Office PowerPoint 2007 object model is far richer than this one simple add-in can demonstrate. To interact with the PowerPoint 2007 object model in code, investigate the documentation listed in Explore It.

Explore It