Using Word.SearchOptions in an Office Add-In

167 views Asked by At

I am developing an Office Add-In that processes the text of each paragraph of a Word document against data in JSON format and writes the result to a within the index.html file that is rendered in the Task Pane. This works fine. I am now trying to format the strings within the Word document that correspond to the hits for the keys in the JSON data.

I have a JS block in the head of the index.html file in which I call "Office.initialize" and define a variable based on the JSON data, and have utility functions related to the above functionality. After that comes a function in which I get the Word context and process the Word file paragraphs against the JSON data, and then try to search the Word paragraph itself in order to format the hits. In this last task I am trying to reproduce a snippet from Michael Mainer 1. But no formatting happens when I activate this function. Unfortunately I don't have access to a console since I am on a Mac, which makes it harder to debug.

I would be very appreciative of someone showing me where I'm going wrong.

`function tester() { Word.run(function (context) {

    // Create a proxy object for the document's paragraphs
    var paragraphs = context.document.body.paragraphs;

    // Load the paragraphs' text, which I run regexes on
    context.load(paragraphs, 'text');

    return context.sync().then(function () {
        for (var i = 0; i < paragraphs.items.length; i++) {
            var text = paragraphs.items[i].text;
            // jquery to iterate over the "notes" objects from the JSON
            $.each(notes, function(key, value) {
                var regex = new RegExp("\\b" + key + "\\b", "g");
                var res = regex.test(text);
                // if the regex hits...
                if (res == true) {
                    // This part works fine, using the JSON data to append to the <DIV> with ID = "notes"
                    document.getElementById('notes').innerHTML += "<button onclick=hide('" + value.seqNo + "')><b>" + key + "</b></button><p class='" + value.seqNo + "' id='" + i + "'>" + value.notes[0].note + "</p>";

                    // I now go on to searching for these hits within the current paragraph in the Word file
                    var thisPara = paragraphs.items[i];

                    // Set up the search options.
                    var options = Word.SearchOptions.newObject(context);
                    options.matchCase = false

                    // Queue the commmand to search the current paragraph for occurrences of the string "key" (coming from the JSON data)
                    var searchResults = paragraphs.items[i].search(key, options);

                    // Load 'text' and 'font' for searchResults.
                    context.load(searchResults, 'text, font');

                    // Synchronize the document state by executing the queued-up commands, and return a promise to indicate task completion.
                    return context.sync().then(function () {

                        // Queue a command to change the font for each found item. 
                        for (var j = 0; j < searchResults.items.length; j++) {
                          searchResults.items[j].font.color = '#FF0000'
                          searchResults.items[j].font.highlightColor = '#FFFF00';
                          searchResults.items[j].font.bold = true;
                        }

                        // Synchronize the document state by executing the queued-up commands, 
                        // and return a promise to indicate task completion.
                        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));
    }
});

} `

1

There are 1 answers

1
bmoran On

It looks like you just need access to the font property, have you tried just:

context.load(searchResults, 'font');

That was working for me?