Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

Create pdf of slide range in vba

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.
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

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?
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

I wanted to automate the task
So I could say how do I make the selection in vba and then print to pdf?
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

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

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

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

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

ASKER CERTIFIED SOLUTION
Avatar of PeterBaileyUk
PeterBaileyUk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial