Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3618
  • Last Modified:

Programmatic PowerPoint Slide Generation

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?
  • 5
  • 4
1 Solution
William ElliottSr Tech GuruCommented:
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.
rhyspAuthor Commented:
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

William ElliottSr Tech GuruCommented:
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
rhyspAuthor Commented:
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 GuruCommented:
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

rhyspAuthor Commented:
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.
William ElliottSr Tech GuruCommented:
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
rhyspAuthor Commented:
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 GuruCommented:
glad to help :)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now