PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Add Title, Keywords (i.e., Tags), Category, and Company Name to a PowerPoint File
Here is a simple Delphi procedure that adds title, keywords (i.e., tags), category, and company information to a Microsoft PowerPoint file. The code uses Microsoft's Presentation.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 PowerPoint automation solutions, see Microsoft PowerPoint Automation with Delphi.
procedure TForm1.AddTitleKeywordsCategoryAndCompanyNameToAPowerPointFile(Sender: TObject);
var
PowerPointFileName: String;
PowerPointApplication, PowerPointPresentation, PresentationProperties: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

PowerPointFileName := 'C:\PhysiologyWeb\delphi_code_examples\powerpoint_file.pptx'; //replace file name with the name of your file

PowerPointApplication := Null;
PowerPointPresentation := Null;
PresentationProperties := Null;

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

If VarIsNull(PowerPointApplication) = False then
begin
try
PowerPointApplication.Visible := True; //set to False if you do not want to see the activity in the background
PowerPointApplication.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 PowerPoint Presentation
try
PowerPointPresentation := PowerPointApplication.Presentations.Open(PowerPointFileName);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentations.open
except
PowerPointPresentation := Null;
//add error/exception handling code as desired
end;

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

If VarIsNull(PresentationProperties) = False then
begin
PresentationProperties.Item['Title'].Value := 'Delphi Code Examples';
PresentationProperties.Item['Keywords'].Value := 'Delphi, Object Pascal, Code'; //this is also referred to as the "tag" property
PresentationProperties.Item['Category'].Value := 'Delphi Programming';
PresentationProperties.Item['Company'].Value := 'PhysiologyWeb';

//use PresentationProperties.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

//save the file
PowerPointPresentation.Save;
//or
//PowerPointApplication.ActivePresentation.Save;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.application.activepresentation
end;
end;
finally
PowerPointPresentation.Close;
PowerPointApplication.DisplayAlerts := True;
PowerPointApplication.Quit;

PresentationProperties := Unassigned;
PowerPointPresentation := Unassigned;
PowerPointApplication := Unassigned;
end;
end;
end;






Posted: Thursday, December 24, 2020