How can I highlight replaced text in a Microsoft Word Document using MATLAB?
6 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2023-2-16
回答: MathWorks Support Team
2023-2-17
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?
采纳的回答
MathWorks Support Team
2023-2-16
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:
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!