PowerPoint VBA To Save Presentation As A PDF In Same Folder

Create PDF From PowerPoint File With VBA Macro Coding

What This VBA Code Does

The business world has increasingly become more reliant on mobile computing with devices such as tablets and smartphones becoming mainstream. This, in turn, has provided an elevated demand for analysts to turn spreadsheets into PDF documents so management can view your reports on the go. 

Below is a simple VBA macro that will allow you to quickly turn your current PowerPoint presentation into a PDF file in a snap. The code is written to save your PDF in the same folder as the PowerPoint file currently resides. If you need to make modifications, hopefully, you will be able to follow along with my code comments and customize the code to your specific needs.

Sub PowerPoint_ExportPDF()
'PURPOSE: Generate A PDF Document With Active Presentation
'NOTES: PDF Will Be Saved To Same Folder As PowerPoint File
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim CurrentFolder As String
Dim FileName As String
Dim myPath As String
Dim UniqueName As Boolean

UniqueName = False

'Store Information About PowerPoint File
  myPath = ActivePresentation.FullName
  CurrentFolder = ActivePresentation.Path & "\"
  FileName = Mid(myPath, InStrRev(myPath, "\") + 1, _
   InStrRev(myPath, ".") - InStrRev(myPath, "\") - 1)

'Does File Already Exist?
  Do While UniqueName = False
    DirFile = CurrentFolder & FileName & ".pdf"
    If Len(Dir(DirFile)) <> 0 Then
      UserAnswer = MsgBox("File Already Exists! Click " & _
       "[Yes] to override. Click [No] to Rename.", vbYesNoCancel)
      
      If UserAnswer = vbYes Then
        UniqueName = True
      ElseIf UserAnswer = vbNo Then
        Do
          'Retrieve New File Name
            FileName = InputBox("Provide New File Name " & _
             "(will ask again if you provide an invalid file name)", _
             "Enter File Name", FileName)
            
          'Exit if User Wants To
            If FileName = "False" Or FileName = "" Then Exit Sub
        Loop While ValidFileName(FileName) = False
      Else
        Exit Sub 'Cancel
      End If
    Else
      UniqueName = True
    End If
  Loop
  
'Save As PDF Document
  On Error GoTo ProblemSaving
    ActivePresentation.ExportAsFixedFormat CurrentFolder & FileName & ".pdf", _
      ppFixedFormatTypePDF, ppFixedFormatIntentPrint, msoCTrue, ppPrintHandoutHorizontalFirst, _
      ppPrintOutputSlides, msoFalse, , ppPrintAll, , False, False, False, False, False
  On Error GoTo 0
  

'Confirm Save To User
  With ActivePresentation
  FolderName = Mid(.Path, InStrRev(.Path, "\") + 1, Len(.Path) - InStrRev(.Path, "\"))
  End With
  
  MsgBox "PDF Saved in the Folder: " & FolderName

Exit Sub

'Error Handlers
ProblemSaving:
  MsgBox "There was a problem saving your PDF. This is most commonly caused " & _
   "by the original PDF file already being open."
  Exit Sub

End Sub

Function To Validate Save File Name

Below is a function that you will need to paste in along with the above macro. The VBA function provides a way of testing any file name your users provide to save the PDF document as.

Function ValidFileName(FileName As Variant) As Boolean
'PURPOSE: Determine If A Given PowerPoint File Name Is Valid
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim ppt As Presentation

'Create a Temporary XLS file
  On Error GoTo InvalidFileName
    Set ppt = Presentations.Add
    ppt.SaveAs Environ("TEMP") & "\" & FileName & ".ppt", ppSaveAsPresentation
  On Error Resume Next

'Close Temp PowerPoint
  ppt.Close

'Delete Temp File
  Kill Environ("TEMP") & "\" & FileName & ".ppt"

'File Name is Valid
  ValidFileName = True

Exit Function

'ERROR HANDLERS
InvalidFileName:
  'Close Temp PowerPoint
    ppt.Close
  
  'File Name is Invalid
    ValidFileName = False

End Function

Same Macro Functionality For Other Office Applications

Upon request, I have made similar macros for other Office Applications you may use on a regular basis to convert their files into PDF documents. The links to those specific posts are as follows:

Microsoft Excel Version

Microsoft Word Version

How Do I Modify This To Fit My Specific Needs?

Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it

I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).

I wish you the best of luck and I hope this tutorial gets you heading in the right direction!

~ Chris