Programmatic PowerPoint Slide Generation

Posted on 2007-07-19
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?
Question by:rhysp
    LVL 19

    Expert Comment

    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
    LVL 21

    Expert Comment

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

    Author Comment

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

    Expert Comment

    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
    LVL 5

    Author Comment

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

    Expert Comment

    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 =
    end if
    emd if

    now do stuff to the placeholder

    LVL 5

    Author Comment

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

    Accepted Solution

    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
    LVL 5

    Author Comment

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

    Expert Comment

    glad to help :)

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Setting the Scene PowerPoint is a creative tool in the right hands but it also includes a much underutilised programming dimension. In this beginner level article, we're going to show you some of some key elements of programming PowerPoint using th…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This video teaches viewers how to fit pictures into slides, crop and remove backgrounds, and alter photos to look more professional.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now