PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Open a Password Protected Excel File and Save As
Here is a simple Delphi procedure that opens an existing password protected Microsoft Excel file (i.e., Excel workbook), makes some modifications to the file, saves the file with a new name (save as), and finally closes the file. In addition to the file name and password, a number of optional parameters may be passed to open the Excel file. 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.OpenAPasswordProtectedExcelFileAndSaveAs(Sender: TObject);
var
ExcelFileName, ExcelFileNameNew, Password: String;
ExcelApplication, ExcelWorkbook, ExcelWorksheet: 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
ExcelFileNameNew := 'C:\PhysiologyWeb\delphi_code_examples\excel_file_new.xlsx'; //replace file name with the name of your file
Password := 'physiologyweb'; //replace with password of your file

ExcelApplication := Null;
ExcelWorkbook := Null;
ExcelWorksheet := 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, EmptyParam, EmptyParam, EmptyParam, Password, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
//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
//connect to Excel Worksheet using either the ExcelApplication or ExcelWorkbook handle
try
ExcelWorksheet := ExcelWorkbook.WorkSheets[1]; //[1] specifies the first worksheet
except
ExcelWorksheet := Null;
//add error/exception handling code as desired
end;

If VarIsNull(ExcelWorksheet) = False then
begin
ExcelWorksheet.Select;

//do what you need with the file before saving with a new name. here we will change the background color for a few cells
ExcelWorksheet.Range['A1:E8'].Interior.Color := clYellow;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.interior.color

ExcelWorksheet.Cells[1,1].Select; //selects Cell A1

ExcelWorkbook.SaveAs(ExcelFileNameNew);
//or
//ExcelApplication.WorkBooks[1].SaveAs(NewExcelFileName);
//Note: If a file with the new name already exists, it overwrites it. Write additional code to address as desired.
//reference
//https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.saveas
end;
end;
finally
ExcelApplication.Workbooks.Close;
ExcelApplication.DisplayAlerts := True;
ExcelApplication.Quit;

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






Posted: Saturday, April 9, 2022