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.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 := '';

ExcelApplication := Null;
ExcelWorkbook := Null;
WorkbookProperties := Null;
VariantValue := 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);
except
ExcelWorkbook := Null;
//add error/exception handling code as desired
end;

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 := 1 to 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;

Memo1.Lines.Add('(' + IntToStr(I) + ') ' + PropertyName + ' = ' + 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;

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

Below is a list of document built-in properties. For additional information, please see the BuiltinDocumentProperties Property page.

Title

Subject

Author

Keywords

Comments

Template

Last Author

Revision Number

Application Name

Last Print Date

Creation Date

Last Save Time

Total Editing Time

Number of Pages

Number of Words

Number of Characters

Security

Category

Format

Manager

Company

Number of Bytes

Number of Lines

Number of Paragraphs

Number of Slides

Number of Notes

Number of Hidden Slides

Number of Multimedia Clips

Hyperlink Base

Number of Characters (with spaces)





Posted: Saturday, October 21, 2023
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