Create pdf of slide range in vba

PeterBaileyUk
PeterBaileyUk used Ask the Experts™
on
I give a class using powerpoint and I save the class progress for that day. After class I use the powerpoint export function to create a pdf from the current selection manually. I would like to do this using vba.

How do I do that.

I have the slide range saved and the db has links to the presentation ppxt itself.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sam JacobsDirector of Technology Development, IPM

Commented:
What do mean that you create the PDF manually? There's no need to export anything.
If you have a PDF print driver installed (e.g. Foxit Reader), You can simply print your selection to the PDF Printer.
I use it almost daily. It works great!

BTW, did I mention that it's free?

Author

Commented:
I wanted to automate the task

Author

Commented:
So I could say how do I make the selection in vba and then print to pdf?
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
If I do this it selects the slides but how do I do this without specifying each value is the array?

I had hoped I could just say select range 10 to 15 as an example.

ActivePresentation.Slides.Range(Array(10, 11, 12, 13, 14, 15)).Select

Open in new window

Author

Commented:
I can loop around the slides I want now but it will only select the last slide.

Public Sub Test()
Dim min, max As Long
min = 10
max = 15
    Dim tempSlide As Slide
    For Each tempSlide In Application.ActivePresentation.Slides
        If tempSlide.SlideNumber >= min And tempSlide.SlideNumber <= max Then
        Debug.Print tempSlide.SlideNumber
            tempSlide.Select
        End If
    Next
End Sub

Open in new window

Author

Commented:
I found this code on the web:
Private Sub CommandButton4_Click()
Dim myInput As String
Dim savePath As String

'Name of Student
myInput = ActivePresentation.Slides(1).Shapes("TextBox2").OLEFormat.Object.Text

'Location of saved file
savePath = ActivePresentation.Path & "\" & myInput & " Antwoorden Virtueel Lab" & ".pdf"  

'Select path student took
If ActivePresentation.Slides(9).Shapes("TextBox1").OLEFormat.Object.Text = "PRARDT" Then

'Change view
ActivePresentation.SlideShowWindow.View.Exit

'Prevents error 
ActiveWindow.Panes(1).Activate

'Select specific slides
ActivePresentation.Slides.Range(Array(9, 11, 15)).Select

********************************************************************************
How do I use variables within the array? Mine are always in sequence but the sequence not fixed ex: 10, 11, 12, 13,14,15 or 
23,24,25,26,27

I can't find anywhere that deals with that, is it possible?

*******************************************************************************************************
'save selected slides as PDF
ActivePresentation.ExportAsFixedFormat Path:=savePath, FixedFormatType:=ppFixedFormatTypePDF, RangeType:=ppPrintSelection

MsgBox "file saved"

Else
MsgBox "wont work"

End If

End Sub

Open in new window

Jamie GarrochSenior Technical Consultant at BrightCarbon

Commented:
try this Peter:

Option Explicit

'----------------------------------------------------------------------------------
' PowerPoint VBA Macro to export selected slides to PDF file.
'----------------------------------------------------------------------------------
' Copyright (c) 2019 BrightCarbon Ltd. All Rights Reserved.
' Source code is provided under Creative Commons Attribution License
' This means you must give credit for our original creation in the following form:
' "Includes code created by BrightCarbon Ltd. (brightcarbon.com)"
' Commons Deed @ http://creativecommons.org/licenses/by/3.0/
' License Legal @ http://creativecommons.org/licenses/by/3.0/legalcode
'----------------------------------------------------------------------------------
' Purpose : Answer to Experts Exchange question 29164957
' https://www.experts-exchange.com/questions/29164957/Create-pdf-of-slide-range-in-vba.html
' Author : Jamie Garroch
' Date : 26NOV2019
' Website : https://brightcarbon.com/
'----------------------------------------------------------------------------------

Sub SaveSelectedSlidesAsPDF()
  Dim tPath As String
  Dim tFilename As String
  
  On Error GoTo errorhandler
  
  With ActiveWindow.Selection
    ' Check the selection is valid
    If .Type <> ppSelectionSlides Then
      MsgBox "Please select one or more slides from the thumbnail pane in the normal view.", vbInformation + vbOKOnly, "Slide Selection to PDF"
      Exit Sub
    End If
  End With
  
  With ActivePresentation
    
    ' Create the file path and PDF filename
    tPath = .Path & "\"
    tFilename = Replace(.Name, ".pptm", ".pdf")
    
    ' Export the selected slides to PDF
    .ExportAsFixedFormat2 Path:=tPath & tFilename, _
                          FixedFormatType:=ppFixedFormatTypePDF, _
                          Intent:=ppFixedFormatIntentPrint, _
                          FrameSlides:=msoFalse, _
                          HandoutOrder:=ppPrintHandoutHorizontalFirst, _
                          OutputType:=ppPrintOutputOneSlideHandouts, _
                          PrintHiddenSlides:=msoFalse, _
                          RangeType:=ppPrintSelection
    
    MsgBox tFilename & " created here:" & vbCrLf & vbCrLf & tPath, vbInformation + vbOKOnly, "PDF Created"
  End With
  
errorhandler:
  If Err <> 0 Then MsgBox "Error " & Err & vbCrLf & Err.Description, vbCritical + vbOKOnly, "Error"
  On Error GoTo 0
End Sub

Open in new window

Author

Commented:
I have the range of slides that I need in variables is it possible to select the required files from that or has a manual selection got to take place?
I will try a different approach. I can select each slide within a range with this code:
Sub Auto_Select()


    Dim Sld As Slide
    Dim Istart As Integer
    Dim Iend As Integer

        'Simply loop through slide nos I could not Understand Range(Array()) part
        Istart = 2   'change according to yuor need
        Iend = 4     'change according to yuor need

        For X = Istart To Iend
        Set Sld = ActivePresentation.Slides(X)
        Sld.Select

        Next X
        End Sub

Open in new window


How can I send each sld to a newly created pdf?
I cant select multiple slides in code but I can rotate through my range now.
I finally got it working:

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 lngStart As Long
Dim savePath As String
Dim PrintPDF As Integer
lngStart = 5
lngLast = 10
'Location of saved file
savePath = Environ("USERPROFILE") & "\Desktop\" & Format(timestamp, "yyyymmdd-hhnn") & "_" & FirstNameX & "_" & LastNameX & ".pdf"

'lngLast = ActivePresentation.Slides.count

With ActivePresentation.PrintOptions
    .Ranges.ClearAll ' always do this
    Set PR = .Ranges.Add(Start:=lngStart, 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

Open in new window

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 lngStart As Long
Dim savePath As String
Dim PrintPDF As Integer
lngStart = 5
lngLast = 10
'Location of saved file
savePath = Environ("USERPROFILE") & "\Desktop\" & Format(timestamp, "yyyymmdd-hhnn") & "_" & FirstNameX & "_" & LastNameX & ".pdf"

'lngLast = ActivePresentation.Slides.count

With ActivePresentation.PrintOptions
    .Ranges.ClearAll ' always do this
    Set PR = .Ranges.Add(Start:=lngStart, 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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial