How can I highlight replaced text in a Microsoft Word Document using MATLAB?

2 views (last 30 days)
I am writing a script using the MATLAB 'actxserver' object to create a Word Application COM Server to work with a Word Document like so:
 
word = actxserver('Word.Application');
word.Documents.Open(fpath);
I am trying to find instances of a string in the document, replace them all with a different string, and highlight the new instances of the replaced word. How can I do this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 16 Feb 2023
While it is possible to change many parameters of the 'Replacement' text, including whether or not the replaced text is highlighted, it is not possible to directly set the color of the highlighting itself in this case.
As a result, it is a bit more complicated to highlight replaced instances of a string only, meaning no instances of the replacement string that were in the document prior to the replacement operation will be highlighted. However, it is possible by utilizing the document 'Options' object, and changing the 'DefaultHighlightColorIndex'. This can be done in MATLAB like so:
options = word.Options;
% Set the default highlight color
options.DefaultHighlightColorIndex = <insert WdColorIndex enumerator value here>;
% Activate the replacement text to be highlighted
selection.Find.Replacement.Highlight = 1;
 
For a full list of WdColorIndex enumerator values, please see the following Microsoft documentation page:

More Answers (0)

Categories

Find more on COM Component Integration in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!