PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Add Blank Slide to an Existing PowerPoint File
Here is a simple Delphi procedure that opens an existing Microsoft PowerPoint file, adds a new blank slide, saves the file with a new name (save as), and finally closes the file. This example adds a blank slide. Other PowerPoint slide layout types are also available and may be used. This procedure is useful when utilizing a PowerPoint template file (with company logo and other brand formatting already included) 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.AddBlankSlideToAnExistingPowerPointFile(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.Slides.Add(2, 12);
//2 = index position of new slide
//12 = ppLayoutBlank
///https://docs.microsoft.com/en-us/office/vba/api/powerpoint.ppslidelayout
PowerPointApplication.ActivePresentation.Slides.Item(2).Select;



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: Sunday, February 27, 2022