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

6 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 16 de Feb. de 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:

Más respuestas (0)

Categorías

Más información sobre Use COM Objects in MATLAB en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by