To save multiple PowerPoint presentation as a separate pdf file in VBA Excel.
In this PowerPoint automation ,we are converting multiple PowerPoint presentations to pdf file. To do so, we select a source folder by dialog box. This source folder contains all the .pptx files. After that, we select a target folder, where all the output pdf files will be saved. All the slides in each PowerPoint will be converted to pdf file, which means each slide will be transformed to a single pdf page.
VBA code is written in a macro file (.xlsm), where we have created a command button. This command button is linked with the below shown VBA code.
1.Add the required library reference as shown in the image below.
2.Display a message box to select the source data folder using dialog box
3.We have used FileDialog method to select the source data folder
4.Save the name of folder in OpenPptFilesFolder using “.SelectedItems(1)” property of FileDialog method
5.Similarly select the target folder, where we want to save the output pdf files
6.Loop through each .pptx file in the source data folder.
7.Use ExportAsFixedFormat method to save the .pptx files in pdf format in target folder
8. Close the PowerPoint application.
Sub ExportPPTSlidesToPDF() Application.DisplayAlerts = False Dim obPptApp As PowerPoint.Application Dim OpenPptDialogBox As Object Dim SavePdfDialogBox As Object Dim OpenPptFilesFolder As String Dim MyPptFile As String Dim MyOpenedPpt As PowerPoint.Presentation Dim PptActualLocation As String Dim MyPdfFileActualName As String Set obPptApp = CreateObject("PowerPoint.Application") Set OpenPptDialogBox = obPptApp.FileDialog(msoFileDialogFolderPicker) Set SavePdfDialogBox = obPptApp.FileDialog(msoFileDialogFolderPicker) 'Select input data folder MsgBox ("Select a --SOURCE data folder -- where input ppt files are located") Set OpenPptDialogBox = obPptApp.FileDialog(msoFileDialogFolderPicker) If OpenPptDialogBox.Show = -1 Then OpenPptFilesFolder = OpenPptDialogBox.SelectedItems(1) End If 'Select output folder Windows(ThisWorkbook.Name).Visible = True AppActivate Application.Caption MsgBox ("Select a --TARGET folder i.e. where output PDF files will be saved") If SavePdfDialogBox.Show = -1 Then SavePdfFilesFolder = SavePdfDialogBox.SelectedItems(1) End If 'Looping through only pptx file in input folder or source data MyPptFile = Dir(OpenPptFilesFolder & "\*.pptx") While MyPptFile <> "" PptActualLocation = OpenPptFilesFolder & "\" & MyPptFile Set MyOpenedPpt = obPptApp.Presentations.Open(PptActualLocation) MyPdfFileActualName = SavePdfFilesFolder & "\" & Replace(obPptApp.ActivePresentation.Name, "pptx", "pdf") obPptApp.ActivePresentation.ExportAsFixedFormat MyPdfFileActualName, FixedFormatType:=ppFixedFormatTypePDF MyOpenedPpt.Close MyPptFile = Dir Wend 'Close the ppt obPptApp.Presentations.Application.Quit End Sub
First we shall add the libraries in VB editor as shown below.
To demonstrate this code, we had created 2 folders.One folder contains the source ppt files, while the other folder is created to contain output pdf files.
We have assigned this macro code to command button in the macro(xlsm) file.[Ref. to image below].We shall click on this command button to execute this macro code.Once we click on this button, a dialog box will appear, there we need to select the source file folder. Then another dialog box will appear, and now we shall select the output folder.Please see the code mentioned in this post.
Before executing the VBA code, source folder had 2 ppt files,our aim is to convert these 2 ppt files into pdf files.
Once the code finishes its execution, both the ppt files have been converted into pdf files now. Both the converted pdf files are saved in output folder as we can see in the image below.
1. We can also declare the variables for source folder and target folders in the code, but using a dialog box
makes it more dynamic.
2. There are multiple options which are available, by using those options we can customize the saving option
according to our need.
Post you may like