How to add pictures to Powerpoint Presentation Picture PlaceHolder?

后端 未结 2 545
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 22:34

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

2条回答
  •  深忆病人
    2021-01-21 23:03

    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.

提交回复
热议问题