copy a slide of a powerpoint presentation in Excel/VBA

scylllla

New Member
Joined
May 7, 2014
Messages
7
Hi,

Im trying to copy a slide in a ppt using the following code:

Code:
Sub openppt()
    Dim PowerPointApp As PowerPoint.Application
    Set PowerPointApp = CreateObject("PowerPoint.Application")
    
    PowerPointApp.Presentations.Open ("C:\test\template.pptx")
    ActivePresentation.Slides(1).Duplicate
End Sub

the ppt opens correctly but everything I trying to do with it results in a error:

"activex component can't create object"

what is going wrong here?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You'll need to qualify your reference...

Code:
PowerPointApp.[COLOR=#574123]ActivePresentation.Slides(1).Duplicate[/COLOR]

Also, since you're using early binding, you can use the New keyword instead of CreateObject to create an instance of the PowerPoint Application object...

Code:
Set PowerPointApp = New PowerPoint.Application

Also, your code can be re-written as follows...

Code:
Sub openppt()
    Dim PowerPointApp As PowerPoint.Application
    Set PowerPointApp = New PowerPoint.Application
    
    Dim PowerPointPres As PowerPoint.Presentation
    Set PowerPointPres = PowerPointApp.Presentations.Open("C:\test\template.pptx")
    
    PowerPointPres.Slides(1).Duplicate
End Sub

Hope this helps!
 
Last edited:
Upvote 0
That helps a lot. Thx! Extra question: How do I place the new slide at the right destination, I mean I want to put it at the end or just as number 7.
 
Upvote 0
To move the duplicate slide after the last slide in the presentation, try...

Code:
Sub openppt()
    Dim PowerPointApp As PowerPoint.Application
    Set PowerPointApp = New PowerPoint.Application
    
    Dim PowerPointPres As PowerPoint.Presentation
    Set PowerPointPres = PowerPointApp.Presentations.Open("C:\test\template.pptx")
    
    Dim PowerPointSlideRange As PowerPoint.SlideRange
    Set PowerPointSlideRange = PowerPointPres.Slides(1).Duplicate
    
    With PowerPointPres
        PowerPointSlideRange(1).MoveTo toPos:=.Slides.Count
    End With
    
End Sub

To move the duplicate slide to a specific location, let's say 7, replace...

Code:
    With PowerPointPres
        PowerPointSlideRange(1).MoveTo toPos:=.Slides.Count
    End With

with

Code:
PowerPointSlideRange.MoveTo toPos:=7

Alternatively, instead of using the Duplicate method, you can use copy/paste. So, for example, to copy the first slide and paste it after the last slide in the presentation...

Code:
    With PowerPointPres
        .Slides(1).Copy
        .Slides.Paste
    End With

To place it at a specific location...

Code:
    With PowerPointPres
        .Slides(1).Copy
        .Slides.Paste Index:=7
    End With

Hope this helps!
 
Upvote 0
With PowerPointPres
PowerPointSlideRange(1).MoveTo toPos:=.Slides.Count
End With

This moves slide 1 to the last position isn't it? this works because slide 1 and the new slide are equal. But it's not the duplicated slide. normally that would be slide 2. But we aren't sure about that.I would expect I could give the destination in the duplicate commando. But I can try the copy paste variant you gave me. Im going to give it a try
 
Upvote 0
You'll need to qualify your reference...

Code:
PowerPointApp.[COLOR=#574123]ActivePresentation.Slides(1).Duplicate[/COLOR]

Also, since you're using early binding, you can use the New keyword instead of CreateObject to create an instance of the PowerPoint Application object...

Code:
Set PowerPointApp = New PowerPoint.Application

Also, your code can be re-written as follows...

Code:
Sub openppt()
    Dim PowerPointApp As PowerPoint.Application
    Set PowerPointApp = New PowerPoint.Application
   
    Dim PowerPointPres As PowerPoint.Presentation
    Set PowerPointPres = PowerPointApp.Presentations.Open("C:\test\template.pptx")
   
    PowerPointPres.Slides(1).Duplicate
End Sub

Hope this helps!
Finally, I found the solution, thanks!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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