PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Search and Replace Text in Microsoft PowerPoint File
Here is a simple Delphi procedure that opens an existing Microsoft PowerPoint file, searches for some target text and replaces it with new desired text, saves the file with a new name (save as), and finally closes the PowerPoint file. Quite a few considerations need to take place to ensure all text is searched within PowerPoint slides and shapes, searching through all textframes, tables, and smartart elements. Microsoft PowerPoint allows for a number of parameters for specialized searches. There are five (5) parameters: FindWhat (required), ReplaceWhat (required), After (optional), MatchCase (optional), WholeWords (optional). 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.
It seems to us that the required code is very complicated. By contrast, Microsoft Word allows for a much simpler approach with Find.Execute (see Search and Replace Text in Microsoft Word). If anyone out there knows a better approach, please let us know.
procedure TForm1.SearchAndReplaceTextInMicrosoftPowerPointFile(Sender: TObject);
var
A, B, R, C, N: Integer;
PowerPointFileName, PowerPointFileNameNew, TargetText, ReplacementText: String;
PowerPointApplication, PowerPointPresentation, PowerPointSlide, PowerPointShape: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

A := 0;
B := 0;
R := 0;
C := 0;
N := 0;

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

TargetText := 'target text';
ReplacementText := 'replacement text';

PowerPointApplication := Null;
PowerPointPresentation := Null;
PowerPointSlide := Null;
PowerPointShape := 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
//iterate through all the slides
For A := 1 to PowerPointPresentation.Slides.Count do
begin
//work with slides that have one or more shapes
If PowerPointPresentation.Slides.Item(A).Shapes.Count > 0 then
begin
PowerPointSlide := Null;
PowerPointSlide := PowerPointPresentation.Slides.Item(A);
//iterate through all the slide shapes
For B := 1 to PowerPointSlide.Shapes.Count do
begin
PowerPointShape := Null;
PowerPointShape := PowerPointSlide.Shapes.Item(B);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.slide.shapes

//textframe
If PowerPointShape.HasTextFrame = True then
begin
If PowerPointShape.TextFrame.HasText = True then
begin
PowerPointShape.TextFrame.TextRange.Replace(TargetText, ReplacementText);
end;
end;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/PowerPoint.TextRange.Replace
//parameters
//Name Required/Optional Data type Description
//FindWhat Required String The text to search for.
//ReplaceWhat Required String The text you want to replace the found text with.
//After Optional Integer The position of the character (in the specified text range) after which you want to search for the next occurrence of FindWhat. For example, if you want to search from the fifth character of the text range, specify 4 for After. If this argument is omitted, the first character of the text range is used as the starting point for the search.
//MatchCase Optional MsoTriState Determines whether a distinction is made on the basis of case.
//WholeWords Optional MsoTriState Determines whether only whole words are found.

//table
If PowerPointShape.HasTable = True then
begin
For R := 1 to PowerPointShape.Table.Rows.Count do
begin
For C := 1 to PowerPointShape.Table.Columns.Count do
begin
PowerPointShape.Table.Cell(R,C).Shape.TextFrame.TextRange.Replace(TargetText, ReplacementText);
end;
end;
end;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/PowerPoint.Table.Cell

//smartart
If PowerPointShape.HasSmartArt = True then
begin
If PowerPointShape.SmartArt.Nodes.Count > 0 then
begin
For N := 1 to PowerPointShape.SmartArt.Nodes.Count do
begin
PowerPointShape.SmartArt.Nodes.Item(N).TextFrame2.TextRange.Replace(TargetText, ReplacementText);
end;
end;
end;
//references
//https://docs.microsoft.com/en-us/office/vba/api/office.smartart
//https://docs.microsoft.com/en-us/office/vba/api/office.smartart.nodes

PowerPointShape := Null;
end;

PowerPointSlide := Null;
end;
end;

PowerPointPresentation.SaveAs(PowerPointFileNameNew);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.saveas
end;
finally
PowerPointPresentation.Close;
PowerPointApplication.DisplayAlerts := True;
PowerPointApplication.Quit;

PowerPointShape := Unassigned;
PowerPointSlide := Unassigned;
PowerPointPresentation := Unassigned;
PowerPointApplication := Unassigned;
end;
end;
end;






Posted: Monday, September 5, 2022