PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Open an Existing PowerPoint File and Save with a New Name (Save As)
Here is a simple Delphi procedure that opens an existing Microsoft PowerPoint file, saves the file with a new name (save as), and finally closes the file. No changes are made to the file before saving with a new name. This is useful when utilizing a PowerPoint template file to start a new presentation. The most relevant parts of the code are highlighted. Comments are included to explain the code. If you copy and paste the code into your program, be sure to change the form and procedure names to match your setup. For additional PowerPoint automation solutions, see Microsoft PowerPoint Automation with Delphi.
procedure TForm1.OpenAnExistingPowerPointFileAndSaveAs(Sender: TObject);
var
PowerPointFileName, PowerPointFileNameNew: String;
PowerPointApplication, PowerPointPresentation: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

PowerPointFileName := 'C:\PhysiologyWeb\delphi_code_examples\powerpoint_file.pptx'; //replace file name with the name of your file
PowerPointFileNameNew := 'C:\PhysiologyWeb\delphi_code_examples\powerpoint_file_new.pptx'; //replace file name with the name of your file

PowerPointApplication := Null;
PowerPointPresentation := Null;

try
//create PowerPoint OLE
PowerPointApplication := CreateOleObject('PowerPoint.Application');
except
PowerPointApplication := Null;
//add error/exception handling code as desired
end;

If VarIsNull(PowerPointApplication) = False then
begin
try
PowerPointApplication.Visible := True; //set to False if you do not want to see the activity in the background
PowerPointApplication.DisplayAlerts := False; //ensures message dialogs do not interrupt the flow of your automation process. May be helpful to set to True during testing and debugging.

//Open PowerPoint Presentation
try
PowerPointPresentation := PowerPointApplication.Presentations.Open(PowerPointFileName);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentations.open
except
PowerPointPresentation := Null;
//add error/exception handling code as desired
end;

If VarIsNull(PowerPointPresentation) = False then
begin
PowerPointApplication.ActivePresentation.SaveAs(PowerPointFileNameNew);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.application.activepresentation
end;
finally
PowerPointApplication.ActivePresentation.Close;
PowerPointApplication.DisplayAlerts := True;
PowerPointApplication.Quit;

PowerPointPresentation := Unassigned;
PowerPointApplication := Unassigned;
end;
end;
end;






Posted: Saturday, December 5, 2020