1
votes

I'm trying to copy slides from one PowerPoint presentation to another with the use of a VBA script in excel. Found the below code but when trying to run it I receive the following error enter image description here

I'm working on 64 bit machine and I'm using the following references: -Visual Basic For Applications -Microsoft Excel 16.0 Object Library -OLE Automation -Microsoft Office 16.0 Object Libarary -Microsoft PowerPoint 16.0 Object Library

Dim objPresentation As Presentation
Dim i As Integer

'open the target presentation
Set objPresentation = Presentations.Open("C:\Users\john\Desktop\123.pptx")
For i = 1 To objPresentation.Slides.Count
    objPresentation.Slides.Item(i).Copy
    Presentations.Item(1).Slides.Paste
    Presentations.Item(1).Slides.Item(Presentations.Item(1).Slides.Count).Design = _
        objPresentation.Slides.Item(i).Design
Next i
objPresentation.Close
End Sub

Could someone please help me overcome this error?

2

2 Answers

2
votes

You have not declared a Powerpoint application object.

Dim objPowerPoint As New PowerPoint.Application
Dim objPresentation As Presentation
Dim i As Integer

'open the target presentation
Set objPresentation = objPowerPoint.Presentations.Open("C:\Users\john\Desktop\123.pptx")

'~~> Rest of the code
0
votes

Working code below

Sub Example2()
Dim objPowerPoint As New PowerPoint.Application
Dim objPresentation As Presentation
Dim i As Integer

'open the target presentation
Set objPresentation = objPowerPoint.Presentations.Open("C:\Users\john\Desktop\p123.pptx")
For i = 1 To objPresentation.Slides.Count
    objPresentation.Slides.Item(i).Copy
    objPowerPoint.Presentations.Item(1).Slides.Paste
    objPowerPoint.Presentations.Item(1).Slides.Item(objPowerPoint.Presentations.Item(1).Slides.Count).Design = _
        objPresentation.Slides.Item(i).Design
Next i
objPresentation.Close
End Sub