Delphi Code - Read and/or Write Microsoft Word File Built-In Properties
Here is a simple Delphi procedure that opens an existing Microsoft Word 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 Word automation solutions, see Microsoft Word Automation with Delphi.
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 := '';
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 := 1to 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;
//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;