Can someone please help me with the VBA Code to Copy Slides 1 to Slide 20 (I have a ppt with 450 slides)and save as a new presentation (New Presentation name should be XYZ.pptx
I have tried
Sub ExportSlides()
Dim myPresentation As Presentations
Set myPresentation = Presentations("PPTWITH450SLIDES.pptx").Slides.range(Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))
myPresentation.Export ("C:\Users\rajat.kapoor\Droom Overview.pptx",FilterName:="pptx")
End Sub
but it is giving compile error: Syntax Error in the last line
myPresentation.Export ("C:\Users\rajat.kapoor\Droom Overview.pptx",FilterName:="pptx")
CodePudding user response:
It's usually simpler to delete unwanted slides from a saved copy of your original presentation. Like so:
Option Explicit
Sub ExportSlides()
Dim x As Long
' Presentation not PresentationS
Dim myPresentation As Presentation
' If the presentation is already open:
' Set myPresentation = Presentations("exportable.pptx")
' otherwise
Set myPresentation = Presentations.Open("c:\temp\exportable.pptx")
myPresentation.SaveAs ("c:\temp\exported.pptx")
' The current presentation is now Exported.pptx
With myPresentation
' change 5 to the highest number slide you want to include 1
For x = .Slides.Count To 6 Step -1
.Slides(x).Delete
Next
End With
myPresentation.Save
End Sub
CodePudding user response:
You get the syntax error because you need to remove the parenthesis from
myPresentation.Export ("C:\Users\rajat.kapoor\Droom Overview.pptx",FilterName:="pptx")
As the official documentation describes the Presentation.Export method has no return value. Therfore it is not a function and has no parentesis for the parameters:
myPresentation.Export "C:\Users\rajat.kapoor\Droom Overview.pptx", FilterName:="pptx"
Alternatively you can use the Call
statement (with parenthesis):
Call myPresentation.Export("C:\Users\rajat.kapoor\Droom Overview.pptx", FilterName:="pptx")
For further explanation also see: https://stackoverflow.com/a/56579194/3219613
To do what you want it is easier to copy the entire presentation and delete the unnecessary slides as @SteveRindsberg suggested in his answer. The Export
method is better used for exporting slides into images.