Navigation Menu Search PhysiologyWeb
PhysiologyWeb Logo Search PhysiologyWeb
   
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL
procedure TForm1.PrintAnExcelFileToAdobePDF(Sender: TObject);
var
ExcelFileName, PDF_FileName: String;
ExcelApplication, ExcelWorkbook: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

ExcelFileName := 'C:\PhysiologyWeb\delphi_code_examples\excel_file.xlsx'; //replace file name with the name of your file
PDF_FileName := ChangeFileExt(ExcelFileName, '.pdf'); //change pdf file name as desired

ExcelApplication := Null;
ExcelWorkbook := Null;

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

If VarIsNull(ExcelApplication) = False then
begin
try
ExcelApplication.Visible := True; //set to False if you do not want to see the activity in the background
ExcelApplication.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 Excel Workbook
try
ExcelWorkbook := ExcelApplication.Workbooks.Open(ExcelFileName);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.workbooks.open
except
ExcelWorkbook := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelWorkbook) = False then
begin
ExcelWorkbook.ExportAsFixedFormat(0, PDF_FileName, 0, True, False, 1, 1, False, EmptyParam);
//If the Excel file is blank and there is nothing to print, it will cause an error.
//If a file with the new name already exists, it overwrites it. Write additional code to address as desired.
//parameters:
//Type Required XlFixedFormatType Can be either xlTypePDF (0) or xlTypeXPS (1).
//FileName Optional Variant A string that indicates the name of the file to be saved. You can include a full path, or Excel saves the file in the current folder.
//Quality Optional Variant Can be set to either of the following XlFixedFormatQuality constants: xlQualityStandard (0) or xlQualityMinimum (1).
//IncludeDocProperties Optional Variant Set to True to indicate that document properties should be included, or set to False to indicate that they are omitted.
//IgnorePrintAreas Optional Variant If set to True, ignores any print areas set when publishing. If set to False, uses the print areas set when publishing.
//From Optional Variant The number of the page at which to start publishing. If this argument is omitted, publishing starts at the beginning.
//To Optional Variant The number of the last page to publish. If this argument is omitted, publishing ends with the last page.
//OpenAfterPublish Optional Variant If set to True, displays the file in the viewer after it is published. If set to False, the file is published but not displayed.
//FixedFormatExtClassPtr Optional Variant Pointer to the FixedFormatExt class.
//reference
//https://docs.microsoft.com/en-us/office/vba/api/Excel.Workbook.ExportAsFixedFormat
end;
finally
ExcelApplication.Workbooks.Close;
ExcelApplication.DisplayAlerts := True;
ExcelApplication.Quit;

ExcelWorkbook := Unassigned;
ExcelApplication := Unassigned;
end;
end;
end;




Posted: Saturday, December 5, 2020
Last updated: Wednesday, March 19, 2025
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL