Underline Color Word Web Add-in without Font Color

2019-08-07 08:06发布

问题:

I am working on Word web addin OfficeJS, I wanna change my underline color to red. Is it possible to change the color of underline without affecting the font color ? Attached my code below:

Word.run(function (context) {

            var searchResults = context.document.body.search(searchResult, { ignorePunct: true });
            context.load(searchResults, 'font');
            return context.sync().then(function () {
                for (var i = 0; i < searchResults.items.length; i++) {

                    searchResults.items[i].font.color = 'red';                   
                    searchResults.items[i].font.underline = 'wave';  
                }
                return context.sync();
            });
        })

回答1:

You have to first create a custom character style with the underline color set to red. Give the style a name. The following code works for me. "StyleZZ" is a character style which specifies an underline font with red underline color. In all other respects it is default font.

var searchResults = context.document.body.search(searchResult, { ignorePunct: true });
searchResults.load("style");
return context.sync().then(function () {
    for (var i = 0; i < searchResults.items.length; i++) {
       searchResults.items[i].style = "StyleZZ";
    }
    return context.sync();
});

Notice that you don't have to load everything on the searchResults object, only the style property.