PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Save Microsoft PowerPoint File as PDF File
Here is a simple Delphi procedure that opens an existing Microsoft PowerPoint file, saves the file as a PDF file, and finally closes the PowerPoint file. No changes are made to the PowerPoint file before saving in PDF format. 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.SaveMicrosoftPowerPointFileAsPDFFile(Sender: TObject);
var
PowerPointFileName, PDFFileName: 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
PDFFileName := ChangeFileExt(PowerPointFileName, '.pdf');

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
PowerPointPresentation.SaveAs(PDFFileName, 32); //ppSaveAsPDF = 32
//or
//PowerPointApplication.ActivePresentation.SaveAs(PDFFileName, 32);
//If a pdf file with the same name already exists, it overwrites it. Write additional code to address as desired.
//parameters:
//Name Required/Optional Data type Description
//FileName Required String Specifies the name to save the file under. If you don't include a full path, PowerPoint saves the file in the current folder.
//FileFormat Optional PpSaveAsFileType Specifies the saved file format. If this argument is omitted, the file is saved in the default file format (ppSaveAsDefault).
//EmbedFonts Optional MsoTriState Specifies whether PowerPoint embeds TrueType fonts in the saved presentation.
//see references below for additional optional parameters
//references
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.saveas
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.ppsaveasfiletype
end;
finally
PowerPointPresentation.Close;
PowerPointApplication.DisplayAlerts := True;
PowerPointApplication.Quit;

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






Posted: Sunday, September 4, 2022