Highlight all occurrences of a string in Word using VSTO

浪尽此生 提交于 2019-12-09 23:27:21

问题


I would like to highlight all occurrences of a given string in Microsoft Word 2010 using VSTO.

So far, I've managed to set the foreground color for the matches using the Find facility:

Word.Find find = Application.ActiveDocument.Content.Find;
find.Replacement.Font.ColorIndexBi = Word.WdColorIndex.wdYellow;
find.Execute(FindText: "dog", MatchCase: false, Replace: Word.WdReplace.wdReplaceAll);

However, I would like to set the highlight for the matches, such as in the screenshot below:


回答1:


Set the highlight colour by setting using

Application.Options.DefaultHighlightColorIndex 

to one of the wdColorIndex members (e.g. wdYellow)

Apply the Highlight colour in a Replace using

find.Replacement.Highlight = True



回答2:


If one wants temporary highlighting, one may use the HitHighlight method instead:

Word.Find find = Application.ActiveDocument.Content.Find;
find.HitHighlight(
    FindText: "dog",
    MatchCase: false,
    HighlightColor: Word.WdColor.wdYellow);


来源:https://stackoverflow.com/questions/21063419/highlight-all-occurrences-of-a-string-in-word-using-vsto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!