word - highlight search results without permanently changing document formatting

余生颓废 提交于 2019-12-04 15:28:06

right now the only way you can achieve your scenario is by traversing the search results collection and change the highlight color for each range like I am showing on the snippet below. To undo this operation you need to do the search again and restore the highlights to white.

Word.run(function (context) {

            // Queue a command to search the document
            var searchResults = context.document.body.search('string you want to search for');
            context.load(searchResults, 'font');
            return context.sync().then(function () {
                console.log('Found count: ' + searchResults.items.length);

                // Queue a set of commands to change the font for each found item.
                for (var i = 0; i < searchResults.items.length; i++) {
                   
                    searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
                   
                }

                return context.sync();
            });
        })
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!