PhysiologyWeb Logo  Search
Share on Facebook Share on Twitter Share on LinkedIn Share on Pinterest Email Copy URL
PhysiologyWeb Loading...

Delphi Code - Read and/or Write Microsoft PowerPoint File Built-In Properties
Here is a simple Delphi procedure that opens an existing Microsoft PowerPoint 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 PowerPoint automation solutions, see Microsoft PowerPoint Automation with Delphi.
procedure TForm1.ReadAndOrWriteMicrosoftPowerPointFileBuiltInProperties(Sender: TObject);
var
I: Integer;
PowerPointFileName, PropertyName, PropertyValue: String;
PowerPointApplication, PowerPointPresentation, PresentationProperties, 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 PowerPoint file property names and values

I := 0;

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

PowerPointApplication := Null;
PowerPointPresentation := Null;
PresentationProperties := Null;
VariantValue := 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
//get BuiltinDocumentProperties
try
PresentationProperties := PowerPointPresentation.BuiltinDocumentProperties;
except
PresentationProperties := Null;
//add error/exception handling code as desired
end;

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

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

//note: if the value is undefined, an exception is thrown
try
VariantValue := PresentationProperties.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:
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';


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


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


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


//now save the file
PowerPointPresentation.SaveAs(PowerPointFileName);
//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;
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