본문 바로가기
Borland

Inserting Images into PowerPoint 2007

by leo21c 2011. 4. 9.
SMALL
http://msdn.microsoft.com/en-us/library/bb870303.aspx

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

    Bb870303.note(en-us,office.12).gifNote:

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

    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

LIST