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!