powerpoint vba export certain slide to pdf
Asked Answered
W

3

4

I am trying to export a selected slide to pdf when calling this function.

This code works brilliantly, but gives me the entire slideshow as a PDF.

Sub Export_to_PDF()
    ActivePresentation.ExportAsFixedFormat ActivePresentation.Path & "\" & "ExportedFile" & ".pdf", ppFixedFormatTypePDF, ppFixedFormatIntentPrint
End Sub

How can I alter the above code so that I can specify a slide number or series of slides to be exported to PDF. I need this code to be able to run from the Slideshow view.

Many thanks.

Wolfson answered 18/7, 2016 at 6:55 Comment(0)
W
3

Ok, so I finally found the answer after months of searching and thought I would share it here plus some additional info based on what I was trying to achieve. Most code is courtesy of random parts found on the net and a bit of my own shonky code.

Sub Generate_PDF_Cert()
'This function saves the last slide as a PDF file with a time stamp and the users name who completed the induction.

timestamp = Now()

Dim PR As PrintRange
Dim lngLast As Long
Dim savePath As String
Dim PrintPDF As Integer


'Location of saved file
savePath = Environ("USERPROFILE") & "\Desktop\Induction\Certificates\" & Format(timestamp, "yyyymmdd-hhnn") & "_" & FirstNameX & "_" & LastNameX & ".pdf"

lngLast = ActivePresentation.Slides.Count

With ActivePresentation.PrintOptions
    .Ranges.ClearAll ' always do this
    Set PR = .Ranges.Add(Start:=lngLast, End:=lngLast)
End With

ActivePresentation.ExportAsFixedFormat _
Path:=savePath, _
FixedFormatType:=ppFixedFormatTypePDF, _
PrintRange:=PR, _
Intent:=ppFixedFormatIntentScreen, _
FrameSlides:=msoTrue, _
RangeType:=ppPrintSlideRange

'Prompt user of file location and option to print.
PrintPDF = MsgBox("A PDF file of this certificate has been saved to: " & vbCrLf & savePath & vbCrLf & vbCrLf & "Would you like to print a copy also?", vbYesNo, "PDF File Created")
If PrintPDF = 6 Then Call Print_Active_Slide


End Sub

So PDF created nice and easy. It basically takes the last slide of the show and exports only that slide to a PDF. This can be changed to a specific slide or range of slides. Then there is an option to also print that selected slide with the following function:

Sub Print_Active_Slide()
' This code determines what slide is currently visible in the
' slide show and then it clears the print range and prints out the
' current slide.


' Declare lSldNum as a long integer.
Dim lSldNum As Long

' Assign lSldNum to the current slide number.
lSldNum = SlideShowWindows(1).View.Slide.SlideNumber

' Set the print options for the active presentation.
With ActivePresentation.PrintOptions

' Set RangeType to print a slide range.
 .RangeType = ppPrintSlideRange

 ' Delete old print range settings.
 .Ranges.ClearAll

 ' Set Ranges to the new range for the current slide.
 .Ranges.Add lSldNum, lSldNum
End With

' Using the current print settings print the slide to the default
' printer.
ActivePresentation.PrintOut

MsgBox "The file has been sent to the default printer", vbOKOnly, "Print Job Sent"

End Sub
Wolfson answered 16/1, 2017 at 15:50 Comment(0)
B
3

Thanks, I had the same problem and here is my solution (keeps hyperlinks in pdf):

Sub Main()

    'Hide all slides
    For i = 1 To ActivePresentation.Slides.Count
        ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoTrue
    Next i

    'Show your slides (for example: slide 2 + 4)
    ActivePresentation.Slides(2).SlideShowTransition.Hidden = msoFalse
    ActivePresentation.Slides(4).SlideShowTransition.Hidden = msoFalse

    'Save location
    Dim filePath As String
    filePath = "C:\Users\YOUR_NAME\Desktop\fileName.pdf"
    ActivePresentation.SaveAs filePath, ppSaveAsPDF

    'Show all slides again
    For i = 1 To ActivePresentation.Slides.Count
        ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoFalse
    Next i

End Sub
Be answered 10/3, 2019 at 15:48 Comment(2)
Great answer. Easier code than what I scrapped together. I think I will use this from now on. Thanks for the new info.Wolfson
If someone wants to save each slide in a separate pdf, I tweaked the answer provided here to fit the slightly different use case (#17929624). Hope it saves somebody's day.Luxuriance
J
2

You can also save selected slides to a PDF file using the following method:

Sub Test()

    'select slides
    ActivePresentation.Slides.Range(Array(1, 2, 4, 6, 9, 11)).Select
    'save selected slides to a PDF file
    ActivePresentation.ExportAsFixedFormat Path:="C:\Path\xxx.pdf", _
        FixedFormatType:=ppFixedFormatTypePDF, _
        Intent:=ppFixedFormatIntentPrint, _
        OutputType:=ppPrintOutputSlides, _
        RangeType:=ppPrintSelection

End Sub

However, if you are calling this function from an Excel VBA, it might fail to get the selected slides. So, if you're calling this function from outside of Powerpoint, be sure to activate the slide preview pane first like this:

    Set pptApp = ActivePresentation.Parent 'Powerpoint Application
    pptApp.ActiveWindow.Panes(1).Activate 'You can skip this if you're already in Powerpoint
Junitajunius answered 4/4, 2020 at 15:22 Comment(1)
This is killer and super simple, thanks! I am having an issue though, and it seems to be related to my deleting the slide in the (2) spot. I want the code to export the slide in the second position (1st after Title), and this has a tendency to work fine until I rearrange the slides: Run-time error Presentation (unknown member): The slides you have selected to print no longer exist. Please make another selection This happens when there clearly IS a slide (2), so perhaps it has something do with some "internal" slide index or something? Any thoughts or tips would be MUCH appreciated.Slipway

© 2022 - 2024 — McMap. All rights reserved.