To save PowerPoint Presentation using VBA Excel.
On a very broader level, here we are opening a presentation from local drive, and once it is opened we are simply saving the PPT using dialog box without making any changes in the .ppt file.
To do so, we had placed a command button on a excel macro file, that command button was assigned the macro code to open and save the PowerPoint presentation. Once we click on that button, the PowerPoint presentation from local drive will be opened. Once the presentation(ppt) is opened, a dialog box will be shown, then we shall save it by using a dialog box.
- Add the required libraries as shown in the image below
- Add the command button on the macro file.
- Assign the below mentioned VBA code to this command button.
- Open the file from local drive
- Open the dialog box to save the PowerPoint presentation
- If we select a location and provide a name to PowerPoint file, it will save the file and close the presentation
- If we do not provide file name, and click the cancel button, then a message box will display, PowerPoint file will close, and the code will exit.
First we shall add the library reference as shown below.
We have added a command button to execute the VBA code mentioned below.
Here we have opened the PowerPoint presentation from fixed path, as we can see in code below.We have used the FileDialog method, which is used for showing “Save As” dialog box.Presentation can be seen in the background.
Finally we shall save the presentation using dialog box with any name.
Sub SavePptByDialogBox() Application.ScreenUpdating = False Dim ObjPptApp As PowerPoint.Application Dim TargetPpt As PowerPoint.Presentation Set ObjPptApp = CreateObject("PowerPoint.Application") Set TargetPpt = ObjPptApp.Presentations.Open("C:\Users\yourusername\Desktop\macro\Test_PPT.pptx") 'Open PPT using dialog box If ObjPptApp.FileDialog(msoFileDialogSaveAs).Show = -1 Then MySaveFileName = ObjPptApp.FileDialog(msoFileDialogSaveAs).SelectedItems(1) Else 'Exit the code, if no file was selected MsgBox "No File Selected" ObjPptApp.Windows(1).Close Exit Sub End If 'Save the PPT file ObjPptApp.ActivePresentation.SaveAs Filename:=MySaveFileName ObjPptApp.Windows(1).Close End Sub
(1) Expression ObjPptApp.FileDialog(msoFileDialogSaveAs).Show has some different functionality. This expression will show the dialog box for “Save As” , if we input a file name and click “save” button,then the value of ObjPptApp.FileDialog(msoFileDialogSaveAs).Show will be -1, if we click “Cancel” then in that case, the value of ObjPptApp.FileDialog(msoFileDialogSaveAs).Show will be 0.
(2) Expression ObjPptApp.FileDialog(msoFileDialogSaveAs).SelectedItems(1) provides us the name of the file which we had entered previously, when the dialog box had displayed. This value will be used when we save the presentation using “SaveAs” method.
Post you may like