PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Print an Excel File to Adobe PDF
Here is a simple Delphi procedure that opens an existing Microsoft Excel file and prints the first worksheet of the workbook to Adobe pdf. 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 Excel automation solutions, see Microsoft Excel Automation with Delphi.
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