Here is a simple Delphi procedure that opens an existing Microsoft Excel file, reads all document built-in properties, and also shows examples of setting the value of document built-in properties. After these operations, the file is saved. 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.ReadAndOrWriteMicrosoftExcelFileBuiltInProperties(Sender: TObject); var I: Integer; ExcelFileName, PropertyName, PropertyValue: String; ExcelApplication, ExcelWorkbook, WorkbookProperties, VariantValue: Variant; begin //be sure ComObj and Variants units are included in the "uses" clause
//be sure a Memo1 VCL is available for outputting the Excel file property names and values
I := 0;
ExcelFileName := 'C:\PhysiologyWeb\delphi_code_examples\excel_file.xlsx'; //replace file name with the name of your file PropertyName := ''; PropertyValue := '';
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.
If VarIsNull(ExcelWorkbook) = False then begin //get BuiltinDocumentProperties try WorkbookProperties := ExcelWorkbook.BuiltinDocumentProperties; except WorkbookProperties := Null; //add error/exception handling code as desired end;
If VarIsNull(WorkbookProperties) = False then begin //for a complete list of BuiltinDocumentProperties, see reference below //https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.workbook.builtindocumentproperties?view=vsto-2017
//use WorkbookProperties.Count to parse through all BuiltinDocumentProperties (Name/Value) and read/write as desired //iterate through all document properties and retrieve the name and value for each property Memo1.Lines.Add('WorkbookProperties.Count = ' + IntToStr(WorkbookProperties.Count)); Memo1.Lines.Add(''); For I := 1to WorkbookProperties.Count do begin PropertyName := ''; PropertyValue := ''; VariantValue := Null;
PropertyName := WorkbookProperties.Item[I].Name;
//note: if the value is undefined, an exception is thrown try VariantValue := WorkbookProperties.Item[I].Value; If VarIsNull(VariantValue) = False then PropertyValue := String(VariantValue); except PropertyValue := ''; end;
//set document property value by name. for example: WorkbookProperties.Item['Title'].Value := 'Delphi Code Examples'; WorkbookProperties.Item['Keywords'].Value := 'Delphi, Object Pascal, Code'; //this is also referred to as the "tag" property WorkbookProperties.Item['Category'].Value := 'Delphi Programming'; WorkbookProperties.Item['Company'].Value := 'PhysiologyWeb';
Memo1.Lines.Add(''); //retrieve document property value by property name. for example: Memo1.Lines.Add('Title = ' + WorkbookProperties.Item['Title'].Value); Memo1.Lines.Add('Keywords = ' + WorkbookProperties.Item['Keywords'].Value); Memo1.Lines.Add('Category = ' + WorkbookProperties.Item['Category'].Value); Memo1.Lines.Add('Company = ' + WorkbookProperties.Item['Company'].Value);
//set document property value by property index. for example: WorkbookProperties.Item[1].Value := 'New Document Title';
Memo1.Lines.Add(''); //read document property namd and value by property index. for example: Memo1.Lines.Add('(1) ' + WorkbookProperties.Item[1].Name + ' = ' + WorkbookProperties.Item[1].Value);
//now save the file ExcelWorkbook.Save; //reference //https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.save end; end; finally ExcelApplication.Workbooks.Close; ExcelApplication.DisplayAlerts := True; ExcelApplication.Quit;