PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Add Title, Keywords (i.e., Tags), Category, and Company Name to a Word File
Here is a simple Delphi procedure that adds title, keywords (i.e., tags), category, and company information to a Microsoft Word file. The code uses Microsoft's Document.BuiltInDocumentProperties to access and edit the built-in document properties. As written, the code edits these properties, however, all the built-in document properties can also be read using a similar approach.
The built-in document properties are: 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), Content type, Content status, Language, Document version.
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.AddTitleKeywordsCategoryAndCompanyNameToAWordFile(Sender: TObject);
var
WordFileName, WordFileNameNew: String;
WordApplication, WordFile, WordFileProperties: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

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

WordApplication := Null;
WordFile := Null;
WordFileProperties := 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
try
WordFileProperties := WordFile.BuiltinDocumentProperties;
except
WordFileProperties := Null;
//add error/exception handling code as desired
end;

If VarIsNull(WordFileProperties) = False then
begin
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';

//use WordFileProperties.Count to parse through all BuiltinDocumentProperties (Name/Value) and read/write as desired
//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
end;

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

WordFileProperties := Unassigned;
WordFile := Unassigned;
WordApplication := Unassigned;
end;
end;
end;






Posted: Sunday, March 20, 2022