How to save PowerPoint Presentation using VBA Excel ?

Save PowerPoint presentation VBA Excel

Objective

To save PowerPoint Presentation using VBA Excel.

Approach

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.

 

Steps

  1. Add the required libraries as shown in the image below
  2. Add the command button on the macro file.
  3. Assign the below mentioned VBA code to this command button.
  4. Open the file from local drive
  5. Open the dialog box to save the PowerPoint presentation
  6. If we select a location and provide a name to PowerPoint file, it will save the file and close the presentation
  7. 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.

 

Save PowerPoint presentation VBA Excel

 

We have added a command button to execute the VBA code mentioned below.

 

Save PowerPoint presentation VBA Excel

 

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.

 

Save PowerPoint presentation VBA Excel

 

Finally we shall save the presentation using dialog box with any name.

 

Save PowerPoint presentation VBA Excel

 

Code

 

 

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

 

Notes

(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.

Reference

https://docs.microsoft.com/en-us/office/vba/api/office.filedialog

Post you may like

Copy or export chart from excel to Powerpoint in VBA Excel | debugvba.com