Replace text on Powerpoint using ActxServer
11 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm using actxserver to access powerpoint, and I would like to replace the 'UUU' text from every slide with a user inputted number. Some slides have UUU in the title, others have it in a body paragraph. Some have multiple instances on one slide. How can I replace them all?
0 个评论
回答(1 个)
Bhanu Prakash
2024-7-19
编辑:Bhanu Prakash
2024-7-19
Hi Sarah,
You can replace all the occurrences of the text ‘UUU’ with a user input by following the workflow mentioned below:
1.Create an 'actxserver' for Powerpoint application and open the presentation:
actPPT = actxserver('PowerPoint.Application');
presentation = actPPT.Presentations.Open('path_to_pptx_file'); % replace 'path_to_pptx_file' with the complete path to the pptx file
2. As you want to replace all the occurrences of ‘UUU’, you will have to iterate through each slide to check if there are any text frames present. If there are any, then you can try replacing ‘UUU’ with the user input. Here is a sample code of how to do it:
% Iterate through each slide in the PPT
for i = 1:presentation.Slides.Count
% Iterate through each shape in the slide to check for any text frames
for j = 1:presentation.Slides.Item(i).Shapes.Count
shape = presentation.Slides.Item(i).Shapes.Item(j);
% Check if the shape has text
if shape.HasTextFrame
% Check if the text frame has text and replace any occurance of
% 'UUU' with user input
if shape.TextFrame.HasText
textRange = shape.TextFrame.TextRange;
textRange.Replace('UUU', userInput);
end
end
end
end
3. Once the replacing is done, you can save and close the presentation using the commands below:
presentation.Save;
presentation.Close;
4. Quit the Powerpoint and delete the server object using:
Quit(actPPT);
delete(actPPT);
If the 'userInput' is a number, then convert it to char and then try replacing it with 'UUU'. Also make sure that the you have edit access to the presentation.
For more information on the 'actxserver', you can refer to the following documenation:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!