We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Programmatic PowerPoint Slide Generation

Medium Priority
Last Modified: 2008-01-15
PowerPoint 2003 on Windows XP Service Pack 2

I want to progammatically create PowerPoint slides using a VBA macro. I just want to use the standard layouts. The slide I'm trying to create at the moment is the one with the title on the top, text on the left and an image on the right. I can fill the title and the text on the left, but I'm having trouble filling the image on the right.

I just want the image to adjust itself automatically to fill the placeholder as if I had clicked Insert > Picture. The AddPicture method requires Left and Top parameters.

Is there a way to access this automatic functionality through VBA code?
Watch Question

William ElliottSr Tech Guru

ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="C:\picture.jpg", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=143, Top:=-14, Width:=434, Height:=570).Select

that is the default width/height
nice code snippet

The full library reference is available at:
And the PowerPoint Developer's Portal is at:

There are quite a few technical articles on how to programmatically create PoweerPoint presentations.


Unfortunately the code puts the image in the slide, but not into the placeholder that is part of the layout, which has me in the same situation as I was before! I've worked around the problem for now by manually (through code) placing the image over the placeholder. I would have really liked to be able to insert it directly into the placeholder, but I couldn't find anything of help in the documentation.

Here's the code I used to get it to place the image in the right position:

With ActiveSlide.Shapes.AddPicture(Photo, msoFalse, msoTrue, 0, 0)
    If .Width > .Height Then
        .Width = ActiveSlide.Shapes.Item(3).Width
        .Left = ActiveSlide.Shapes.Item(3).Left
        .Top = ActiveSlide.Shapes.Item(3).Top + ((ActiveSlide.Shapes(3).Height - .Height) / 2)
        .Height = ActiveSlide.Shapes.Item(3).Height
        .Left = ActiveSlide.Shapes.Item(3).Left + ((ActiveSlide.Shapes(3).Width - .Width) / 2)
        .Top = ActiveSlide.Shapes.Item(3).Top
    End If
End With

(Oh, ActiveSlide is my own variable to refer to the slide I'm working on)

The trouble with this strategy is that I can't change the layout later on and have the image automatically follow the placeholder, which happens when you do it by hand.

If anyone has any idea on how to solve this problem I would really appreciate it.
William ElliottSr Tech Guru

i think this will lead you in the right direction
programatically get the coordinates for everything, then use the information to create your slides

Sub ExportCoords()

    Dim oSlides As Slides
    Dim oSl As Slide
    Dim oSh As Shape
    Dim strOutput As String
    Dim strFileName As String
    Dim intFileNum As Integer
    Dim lngReturn As Long

    ' Get a filename to store the collected text
    strFileName = InputBox("Enter the full path and name of file to save info to", "Output file?")

    ' did user cancel?
    If strFileName = "" Then
        Exit Sub
    End If

    ' is the path valid?  crude but effective test:  try to create the file.
    intFileNum = FreeFile()
    On Error Resume Next
    Open strFileName For Output As intFileNum
    If Err.Number <> 0 Then     ' we have a problem
        MsgBox "Couldn't create the file: " & strFileName & vbCrLf _
            & "Please try again."
        Exit Sub
    End If
    Close #intFileNum  ' temporarily

    strOutput = "Slide" & vbTab & "Name" & vbTab & "Type" _
    & vbTab & "Left" & vbTab & "Top" & vbTab & "width" _
    & vbTab & "height" & vbCrLf

    ' Get the info
    Set oSlides = ActivePresentation.Slides
    For Each oSl In oSlides
        For Each oSh In oSl.Shapes
            strOutput = strOutput _
                & oSl.SlideIndex & vbTab _
                & oSh.Name & vbTab _
    & oSh.Type & vbTab _
                & oSh.Left & vbTab _
                & oSh.Top & vbTab _
                & oSh.Width & vbTab _
                & oSh.Height & vbCrLf
        Next oSh
    Next oSl

    ' now write the text to file
    Open strFileName For Output As intFileNum
    Print #intFileNum, strOutput
    Close #intFileNum

    ' show what we've done
    lngReturn = Shell("NOTEPAD.EXE " & strFileName, vbNormalFocus)

End Sub


Ok. Maybe I am not being as clear as I could be.

I want my generated slides to take full advantage of the slide content layouts in PowerPoint (Format > Slide Layout). The idea of the slides that I generate here is that they are used as a basis for a presentation. I would like to have it so that the user can easily change the layout so that the image is on the left instead of the right, if they want to and to be able to do that after the slides have been generated.

When I manually (by hand) create a slide with this layout the image I insert goes into the placeholder. Because it is in the placeholder I can use the layouts to change where the image is located easily. Recording this to a macro gives code similar to weellio's, it doesn't put it into the placeholder.

I have everything positioned correctly now (using the code I posted above, which positions the picture over the placeholder, but not inside it).

So, is it possible to fill the placeholder with a picture from code?

Looking through the documentation and playing around it looks like it can't be done. I'm hoping that I'm overlooking something.
William ElliottSr Tech Guru

yes, it can be done.
first you need to know
1. the name of the place holder. the code i provided gived you that
2. the location of the placeholder. the code i provided gived you that as well.

programatically utilizing the information you should be able to modify it to your needs.

first run the script against a generic template slid to get the positioning and save that to the side
now modify the script and and plug in the positioning information
if placeholder top = 123 then
if placeholder left = 123 then
set myplaceholder = placeholder.name
end if
emd if

now do stuff to the placeholder


I'm confused. I can access the placeholder, but I can't add a Picture to it. I can make a picture fit to its dimensions (which is how I've worked around the problem so far), but the picture itself is separate from the placeholder.

I can pick the right Placeholder object. In my case, I access it using

I can add text inside the Placeholder:
   ActiveSlide.Shapes.Item(3).TextFrame.TextRange.Text = PhotoFilename

The Placeholder is a Shape object. Unfortunately only the Shapes collection has the AddPicture method, which returns a new Shape object.

I need to link the Placeholder with the Picture that I load in. I have it positioning so it looks fine, but the image doesn't use the placeholder, other than as a guide for working out its position and dimensions.

So, I have access to the right Placeholder (I've had this since the beginning) and I have access to the image I create.

This isn't so much about positioning as about creating better slides. To see what I mean, create a slide with the layout with the text on the left and the image on the right. Pop some text in the left and insert a picture into the placeholder on the right. Now change the layout so the image is on the left instead of the right. When you change the layout it adjusts the placeholders.

When I do it through code, the placeholder and image are independent of each other. I want them to be connected.
Sr Tech Guru
this works you will need to use variables for the picture name and the rectangle number

  ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="C:\picture.jpg", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=143, Top:=-14, Width:=434, Height:=570).Select

    ActiveWindow.Selection.SlideRange.Shapes("Rectangle 10").Select

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Fantastic weellio. Works like a charm. I had tried variation on the cut/paste, but never using View.Paste

Here's what I ended up having in my code:

   ActiveSlide.Shapes.AddPicture(PhotoFile, msoFalse, msoTrue, 0, 0).Select

ActiveSlide is a variable that I use to track the current slide. ppt references the PowerPoint.Application object. (I'm actually running the code from Excel).

It's important to make sure that the slide is selected before you select the picture.

Thanks for your help.
William ElliottSr Tech Guru

glad to help :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.