How to save or convert PowerPoint presentation (.pptx) as PDF file in VBA Excel

Save ppt as pdf in VBA excel

Objective

To save PowerPoint presentation file (.pptx)  as PDF file.

Approach

In this post about PowerPoint automation, we are opening a PowerPoint presentation (ppt file) by using a dialog box. Once the file is opened, we are selecting a output location, where converted .pdf file will be saved. We have also used some message boxes for end user’s convivence to select source file and destination folder. Here name of output pdf file will be same as original .ppt file name.This code is written in a .xlsm file.

Steps

  1. Add the required library as shown in the image
  2. Add a command button in the .xlsm file and assign it the macro code shown below.
  3. Display message box to select a .pptx file
  4. Select a .pptx file using dialog box
  5. Select a location where final .pdf file will be saved.
  6. Name of pdf file will be same as that of original ppt file
  7. Save ppt file as pdf type by using ExportAsFixedFormat function
  8. Close the ppt file

 

Code

Sub ExportPPTSlidesToPDF()
Application.ScreenUpdating = False

Dim obPptApp As PowerPoint.Application
Dim OpenPptDialogBox As Object
Dim SavePptDialogBox As Object
Dim PdfFilePath As String


Set obPptApp = CreateObject("PowerPoint.Application")
Set OpenPptDialogBox = obPptApp.FileDialog(msoFileDialogOpen)

'Display message box to select a PPT
MsgBox ("Select a PPT from any location")


'Open the target PPT using dialog box
If OpenPptDialogBox.Show = -1 Then
obPptApp.Presentations.Open (OpenPptDialogBox.SelectedItems(1))
End If



'Display message box to select a location or folder, where pdf file will be saved
ThisWorkbook.Activate
MsgBox ("Select a location where PDF file will be saved")


'Get the destination location where you want to save your pdf file
Set SavePptDialogBox = obPptApp.FileDialog(msoFileDialogFolderPicker)

If SavePptDialogBox.Show = -1 Then
'Use the active presentation's name, replace is used to change file extension to pdf from pptx
PdfFilePath = SavePptDialogBox.SelectedItems(1) & "\" & Replace(obPptApp.ActivePresentation.Name, "pptx", "pdf")
obPptApp.ActivePresentation.ExportAsFixedFormat PdfFilePath, FixedFormatType:=ppFixedFormatTypePDF
End If

'Close the ppt
obPptApp.Windows(1).Close

End Sub

 

First we shall add the below shown libraries in Visual Basic editor.

 

Save PowerPoint as PDF VBA Excel

 

As we can see that we had assigned this macro code to a command button on .xlsm file.

 

Save PowerPoint as PDF VBA Excel

 

Once we click on the command button, a message box will appear as shown below.

 

Save PowerPoint as PDF VBA Excel

 

Next a dialog box will appear, we shall select a .pptx file by using this, as shown in image below.

 

save ppt to pdf vba excel

 

 

Below shown is the source .pptx file , which we shall convert to pdf file.This source ppt file has 3 slides.

 

Save PowerPoint as PDF VBA Excel

 

Once we have selected the .pptx file, then a dialog box will appear which will ask us where to save the output pdf file.

 

Save ppt as pdf vba excel

 

Once the VBA code is executed completely ,it will convert the .pptx file to pdf file.In the image below you can also see that output pdf file has 3 pages.Since source ppt file has 3 slides , that’s why output pdf file has 3 pages.

Output pdf file will have the same name as that of source .pptx file ,which is test_ppt here.

 

Save PowerPoint as PDF VBA Excel

 

Note

  1. This code is applicable for single ppt file, it can be customized for multiple files as well.
  2. File name and folder can also be hard coded as per requirement.
  3. It provides flexibility for selecting source file from any location.
  4. We can use this to create similar excel or PowerPoint automation tools as per requirement
  5. It also provides the flexibility to save the pdf file anywhere, by using dialog box
  6. Add the error handling code as per your requirement
  7. Similar VBA Excel automation code can be used to convert Microsoft word document to pdf as well

 

Reference

https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.exportasfixedformat


Post you may like

Save multiple PowerPoint presentation (.pptx) as a separate pdf file in VBA Excel