How to add pictures to Powerpoint Presentation Picture PlaceHolder?

10,276

Solution 1

This is how you add pictures in currently open PPT Picture PlaceHolders using Excel.
We used Early Binding adding the Microsoft PowerPoint 14.0 Object Library reference.

Edit1: Adding DoEvents and some explanation

Sub ImportPictureInPlaceHolderFromExcel()

    Dim oPPt As PowerPoint.Application
    Dim oPPtSlide As PowerPoint.Slide
    Dim oPPtShp As PowerPoint.Shape

    '~~> Get hold of PPt instance meaning your currently open PPT presentation
    Set oPPt = GetObject(, "Powerpoint.Application")
    '~~> Reference the first slide which should contain picture placeholders
    Set oPPtSlide = oPPt.ActivePresentation.Slides(1)

    '~~> Now check each shape in slide
    For Each oPPtShp In oPPtSlide.Shapes
        '~~> You only need to work on Picture place holders
        If oPPtShp.PlaceholderFormat.Type = ppPlaceholderPicture Then
            With oPPtShp
                '~~> Now add the Picture
                '~~> For this example, picture path is in Cell A1
                oPPtSlide.Shapes.AddPicture Range("A1").Value, msoFalse, msoTrue, _
                                .Left, .Top, .Width, .Height
                '~~> Insert DoEvents here specially for big files, or network files
                '~~> DoEvents halts macro momentarily until the 
                '~~> system finishes what it's doing which is loading the picture file
                DoEvents
            End With
        End If
    Next

    Set oPPtSlide = Nothing
    Set oPPt = Nothing

End Sub

To sum-up:
1. We get hold of PPT application
2. We get hold of the slide and the shapes within the slide
3. Now we choose shapes which are ppPlaceholderPicture type only
4. We use the Shape Object's(ppPlaceholderPicture type) .Top, .Left, .Width and .Height property as argument for Shapes Collection's .AddPicture method.

And there you go, you've added a picture in your PPT Picture Placeholder.
Hope this is what you need.

Solution 2

While that looks like it works when you add an image to a slide with an empty picture or content placeholder it will always go into that placeholder and resize to fit.

You just need to add it like this:

osld.Shapes.AddPicture "Path", msoFalse, msoTrue, -1, -1
Share:
10,276
pixie
Author by

pixie

Updated on June 04, 2022

Comments

  • pixie
    pixie almost 2 years

    I have created some code in Excel VBA to create a PowerPoint presentation 1 slide for each row of Excel, and populate in a specific text box in PowerPoint.
    I now want to add in all the images that match the description. These are all Jpegs and not charts etc.
    How can I do this, and is it better to do this in excel, or is it better to do this Powerpoint VBA itself?
    Eitherway, would anyone be able to help me out with some code on how to do this please?
    The image frames already exist in PowerPoint. There are 2 images per slide (no transitions or anything).
    Thank you!

    P.S I am using PowerPoint and Excel 2010 on Windows 7.


    Is there a way to do this from Excel? The rest of my code is in Excel and It would be great to do that as part of the Macro.
    Basically I have a file location that I want to use e.g. C:\insertfoldername\imagename.jpeg appears in column H in the spreadsheet (about 400 rows).
    The Powepoint template I am using has the image frame (The one in Powerpoint, that wehn you hover over it says.."Insert Picture from file".
    These are already sized and are in the right location.
    What I want to do is, in Excel, Paste the image from the file path that is in excel and past it into that specific Image frame.
    Is that going to be possible at all?

    Basically something that will do this:
    PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage(spath)

    Below is the code I am using.
    **** Indicates the File path. the jpg file is set as the 3rd column in the excel spreadsheet.

    Sub CreateSlides()
    'Dim the Excel objects
    Dim objWorkbook As New Excel.Workbook
    Dim objWorksheet As Excel.Worksheet
    
    'Dim the File Path String
    Dim strFilePath As String
    
    'Dim the PowerPoint objects
    Dim PPT As Object
    Dim pptSlide As PowerPoint.Slide
    Dim pptLayout As PowerPoint.CustomLayout
    Dim pptNewSlide As PowerPoint.Slide
    Dim str As String
    Dim Title As String
    
    Set PPT = GetObject(, "PowerPoint.Application")
    
    PPT.Visible = True
    
    'Get the layout of the first slide and set a CustomLayout object
    Set pptLayout = PPT.ActivePresentation.Slides(1).CustomLayout
    
    'Run the OpenFile function to get an Open File dialog box. It returns a String containing the file and path.
    strFilePath = OpenFile()
    
    'Open the Excel file
    Set objWorkbook = Excel.Application.Workbooks.Open(strFilePath)
    
    'Grab the first Worksheet in the Workbook
    Set objWorksheet = objWorkbook.Worksheets(1)
    
    'Loop through each used row in Column A
    For i = 2 To objWorksheet.Range("A65536").End(xlUp).Row
    
    Set PPT = GetObject(, "PowerPoint.Application")
    
    Set pptNewSlide = PPT.ActivePresentation.Slides.AddSlide(PPT.ActivePresentation.Slides.Count + 1, pptLayout)
    
     'Get the number of columns in use on the current row
        Dim LastCol As Long
        Dim boldWords As String
    
        boldWords = "Line1: ,line2: ,Line3: ,Line4: "
        LastCol = objWorksheet.Rows(i).End(xlToRight).Column
        If LastCol = 16384 Then LastCol = 1 'For some reason if only column 1 has data it returns 16384, so correct it
    
        'Build a string of all the columns on the row
        str = ""
        str = "Line1: " & str & objWorksheet.Cells(i, 1).Value & Chr(13) & _
        "Line2: " & objWorksheet.Cells(i, 2).Value & Chr(13) & _
        "Line3: " & objWorksheet.Cells(i, 10).Value & Chr(13) & _
        "Line4: " & objWorksheet.Cells(i, 7).Value & Chr(13) & Chr(13) & _
        objWorksheet.Cells(i, 14).Value
    
     sfile = Cells(i, 3) & ".jpg" **** This is the jpg name
    
    Set PPT = GetObject(, "PowerPoint.Application")
    
    spath = "C:\test\"
    
    'Write the string to the slide
    pptNewSlide.Shapes(2).TextFrame.TextRange.Text = objWorksheet.Cells(i, 3).Value 'This enters the film Title
    PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(1).TextFrame.TextRange.Text = str
    
    
    BoldSomeWords PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(1), str, boldWords
    
    'This is where I want to load in the Image.
    'PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(3).Picture = LoadPicture(spath) ' & sfile)
    'PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage((spath))
    
    Next
    End Sub
    

    Function OpenFile()
    'Dim the File Dialog object and string
    Dim objFileDialog As FileDialog
    Dim strFile As String
    
    'Set the objFileDialog to an instance of the FileDialog object
    Set objFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    'Set the Properties of the objFileDialog object
    objFileDialog.AllowMultiSelect = False
    objFileDialog.ButtonName = "Select"
    objFileDialog.InitialView = msoFileDialogViewDetails
    objFileDialog.Title = "Select Excel File"
    objFileDialog.InitialFileName = "C:\"
    objFileDialog.Filters.Clear
    objFileDialog.Filters.Add "Excel", "*.xls; *.xlsx", 1
    objFileDialog.FilterIndex = 1
    
    'Show the FileDialog box
    objFileDialog.Show
    
    'Set strFile to the first record of the SelectedItems property of our FileDialog
    strFile = objFileDialog.SelectedItems(1)
    
    'Return the File Path string
    OpenFile = strFile
    End Function
    
  • pixie
    pixie about 10 years
    Hi. Thank you so much @L42 exactly what I was after :).....If I put the images on a network drive, do I need to do anything special? It works fantastic on my C:\ but won't run over a network drive.
  • L42
    L42 about 10 years
    Ah that is due to network delay of course specially if the server is somewhere offsite or you are using wireless connection instead of LAN. Well you can try to add DoEvents after the .AddPicture statement and see if it works for you. If not, you will have to copy the pictures first in your local drive then run the macro. I think copy paste from drive to drive won't hurt so much. Cheers. Btw, see my edit for the DoEvents.