To save PowerPoint presentation file (.pptx) as PDF file.
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.
- Add the required library as shown in the image
- Add a command button in the .xlsm file and assign it the macro code shown below.
- Display message box to select a .pptx file
- Select a .pptx file using dialog box
- Select a location where final .pdf file will be saved.
- Name of pdf file will be same as that of original ppt file
- Save ppt file as pdf type by using ExportAsFixedFormat function
- Close the ppt file
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.
As we can see that we had assigned this macro code to a command button on .xlsm file.
Once we click on the command button, a message box will appear as shown below.
Next a dialog box will appear, we shall select a .pptx file by using this, as shown in image below.
Below shown is the source .pptx file , which we shall convert to pdf file.This source ppt file has 3 slides.
Once we have selected the .pptx file, then a dialog box will appear which will ask us where to save the output pdf file.
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.
- This code is applicable for single ppt file, it can be customized for multiple files as well.
- File name and folder can also be hard coded as per requirement.
- It provides flexibility for selecting source file from any location.
- We can use this to create similar excel or PowerPoint automation tools as per requirement
- It also provides the flexibility to save the pdf file anywhere, by using dialog box
- Add the error handling code as per your requirement
- Similar VBA Excel automation code can be used to convert Microsoft word document to pdf as well
Post you may like