VBA Copy Pictures from Excel and Paste them in PowerPoint

Magdoulin

Board Regular
Joined
Jan 11, 2013
Messages
73
Hi Guys, how is everything?
I have excel sheet contains 3 pictures, Picture1, Picture2 and Picture3.

How to write VBA code to open specific existing PowerPoint file, delete any pictures in this PowerPoint file first, and then to copy Picture1 and paste in slide 2, Picture2 in slide 5 and Picture3 in slide 8.


P.S. This topic was originally posted in Ozgrid forum with this link:
https://www.ozgrid.com/forum/forum/help-forums/excel-vba-macros/1205805-copy-pictures-from-excel-and-paste-them-in-powerpoint
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
here you go...
Code:
Sub ppt()
Dim oppApp As PowerPoint.Application
Dim oppt As Presentation
Dim osilde As Slide
'(.pptx)


Set oppApp = New PowerPoint.Application
oppApp.Visible = msoCTrue
Set oppt = oppApp.Presentations.Add
Set oslide = oppt.Slides.Add(1, ppLayoutBlank)


'''''''create chart and name it chart2 in sheet2
Sheet2.ChartObjects("Chart 2").Copy


oppApp.ActivePresentation.Slides(1).Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile).Select


'''here you change chart size
With oppApp.ActiveWindow.Selection.ShapeRange
    LockAspectRatio = 1
    .Left = 50
    .Top = 50
    .LockAspectRatio = 0
    .Height = 200
    .Width = 250
End With


End Sub
 
Upvote 0
Great! Thanks
However here's the case
I need to use regularly an existing PowerPoint file with known location path
Not to create one
And the other thing I want the Excel VBA to delete any existing picture first from this PowerPoint file before starting to paste the new ones to it
How to do that?
 
Upvote 0
Code:
Sub PPT_Autom()
Dim ObjPPT As PowerPoint.Application
Dim oPresentation As PowerPoint.Presentation
Dim oslide As PowerPoint.Slide
Dim oshape As PowerPoint.Shape
Dim i As Long
Dim opath As String


opath = "D:\Training\mrexcel\mks1.pptx"
Set ObjPPT = New PowerPoint.Application
ObjPPT.Visible = msoCTrue
Set oPresentation = ObjPPT.Presentations.Open(opath, msoCTrue)
'Set oslide = oPresentation.Slides.Add(oPresentation.Slides.Count + 1, ppLayoutBlank)
'Set oslide = oPresentation.Slides.Add(oPresentation.Slides.Count + 1, ppLayoutBlank)


For Each oslide In oPresentation.Slides
        For Each oshape In oslide.Shapes
            oshape.Delete
        Next oshape
Next oslide


Sheet1.ChartObjects("Chart 1").Copy


ObjPPT.Activate
'''requir to select shape in particular slide
ObjPPT.ActiveWindow.View.GotoSlide (2)
ObjPPT.ActivePresentation.Slides(2).Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select


'''here you change chart size
With ObjPPT.ActiveWindow.Selection.ShapeRange
    .LockAspectRatio = False
    .Left = 50
    .Top = 50
    .LockAspectRatio = False
    .Height = 300
    .Width = 300
End With


Set oslide = Nothing
Set oPresentation = Nothing




End Sub
 
Upvote 0
I have not tested .... just added loop to delete shape/ view.goto line.
 
Upvote 0
replace old one with these

Code:
'''' old one
For Each oslide In oPresentation.Slides
        For Each oshape In oslide.Shapes
            oshape.Delete
        Next oshape
Next oslide
Code:
'--New
Dim i As Long
For Each oSlide In oPresentation.Slides
    For i = oSlide.Shapes.Count To 1 Step -1
            oSlide.Shapes(i).Delete
    Next i
Next oSlide
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,469
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top