1

My code to add a picture to a slide is as follows:

        PowerPoint.Application ppApp = Globals.ThisAddIn.Application;
        PowerPoint.SlideRange ppSR = ppApp.ActiveWindow.Selection.SlideRange;
        PowerPoint.Shape shape = ppSR.Shapes.AddPicture(
            fileName, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue,
            l, t,
            graphicSize,
            graphicSize);

Where filename is my image and l,t and graphic size are all floats which are the same value.

float graphicSize = 50;

float l = 915;

float t = 495;

This code puts the image into a different location depending on the slide template shown below:

Title Slide - Way I would like it displayed enter image description here

Title and Content Slide enter image description here

Two Content enter image description here

I must also add that this method currently throws an error if more than one slide is selected. I don't know if there is a better way to only add to the current slide, or add to all if more than 1 selected.. The error it returns is a poor COM error that gives no feedback

JKennedy
  • 18,150
  • 17
  • 114
  • 198

1 Answers1

1

The problem with placement is because PowerPoint automatically drops inserted images into any available placeholders that can contain images (content or picture placeholders). To get the picture to go where YOU want it to go, you need to either delete the placeholder before inserting the picture or put dummy content into it, insert the picture, then delete the dummy content.

As to the error, you can only add shapes to a single slide at a time. If you want to work with a selection of slides, you'd iterate through the selection, a slide at a time. In VBA it'd be something like:

Dim oSl as Slide
For Each oSl in ActiveWindow.Selection.ShapeRange.Slides
   ' Do your stuff
Next
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34
  • how would I go about deleting the placeholders? – JKennedy Sep 18 '14 at 13:10
  • Ok I've found it. loop through `ppSR.Shapes` using `item.Delete()` on the `Powerpoint.Shape` – JKennedy Sep 18 '14 at 13:19
  • I have now found that using a foreach on `ppSR.Shapes` only enumerates `ppSR.Shapes.Count -1` times... A bug maybe? – JKennedy Sep 18 '14 at 13:51
  • Cracked it. Because you are deleting items from ppSR.Shapes when the foreach comes to reevaluate the Count the count has dropped by 1 (because you've deleted 1) best solution I found is to build a list of items to delete them cycle through those after deleting them – JKennedy Sep 18 '14 at 14:05
  • 1
    Or the fast easy way: iterate through them backwards. For X = Shapes.Count to 1 Step -1 ... etc. – Steve Rindsberg Sep 19 '14 at 13:00
  • i am facing similar issue but i want this on only selected page. now since my template has placeholder it first places image in that placeholder and then to the location given. plz can u help me with code – test Apr 24 '15 at 07:11
  • Do you want to preserve any blank placeholders for later use, or is it ok to delete them before adding the picture? – Steve Rindsberg Apr 24 '15 at 15:27