JKennedy
JKennedy

Reputation: 18799

Adding pictures to slide puts them in different location depending on slide template

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

Upvotes: 1

Views: 1329

Answers (1)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

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

Upvotes: 1

Related Questions