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.ReadAndOrWriteMicrosoftWordFileBuiltInProperties(Sender: TObject);
var
I: Integer;
WordFileName, PropertyName, PropertyValue: String;
WordApplication, WordFile, WordFileProperties, 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 Word file property names and values

I := 0;

WordFileName := 'C:\PhysiologyWeb\delphi_code_examples\word_file.docx'; //replace file name with the name of your file
PropertyName := '';
PropertyValue := '';

WordApplication := Null;
WordFile := Null;
WordFileProperties := Null;
VariantValue := Null;

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

If VarIsNull(WordApplication) = False then
begin
try
WordApplication.Visible := True; //set to False if you do not want to see the activity in the background
WordApplication.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 Word File
try
WordFile := WordApplication.Documents.Open(WordFileName);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/word.documents.open
except
WordFile := Null;
//add error/exception handling code as desired
end;

If VarIsNull(WordFile) = False then
begin
//get BuiltinDocumentProperties
try
WordFileProperties := WordFile.BuiltinDocumentProperties;
except
WordFileProperties := Null;
//add error/exception handling code as desired
end;

If VarIsNull(WordFileProperties) = 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 WordFileProperties.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('WordFileProperties.Count = ' + IntToStr(WordFileProperties.Count));
Memo1.Lines.Add('');
For I := 1 to WordFileProperties.Count do
begin
PropertyName := '';
PropertyValue := '';
VariantValue := Null;

PropertyName := WordFileProperties.Item[I].Name;

//note: if the value is undefined, an exception is thrown
try
VariantValue := WordFileProperties.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:
WordFileProperties.Item['Title'].Value := 'Delphi Code Examples';
WordFileProperties.Item['Keywords'].Value := 'Delphi, Object Pascal, Code'; //this is also referred to as the "tag" property
WordFileProperties.Item['Category'].Value := 'Delphi Programming';
WordFileProperties.Item['Company'].Value := 'PhysiologyWeb';


Memo1.Lines.Add('');
//retrieve document property value by property name. for example:
Memo1.Lines.Add('Title = ' + WordFileProperties.Item['Title'].Value);
Memo1.Lines.Add('Keywords = ' + WordFileProperties.Item['Keywords'].Value);
Memo1.Lines.Add('Category = ' + WordFileProperties.Item['Category'].Value);
Memo1.Lines.Add('Company = ' + WordFileProperties.Item['Company'].Value);


//set document property value by property index. for example:
WordFileProperties.Item[1].Value := 'New Document Title';


Memo1.Lines.Add('');
//read document property namd and value by property index. for example:
Memo1.Lines.Add('(1) ' + WordFileProperties.Item[1].Name + ' = ' + WordFileProperties.Item[1].Value);

//now save the file
WordFile.Save;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/word.documents.save
end;
end;
finally
WordFile.Close;
WordApplication.DisplayAlerts := True;
WordApplication.Quit;

WordFileProperties := Unassigned;
WordFile := Unassigned;
WordApplication := 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: Sunday, October 15, 2023
Last updated: Tuesday, March 18, 2025
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL