Sk Asif
Sk Asif

Reputation: 75

Couldn't search array element in context.document.body.search - WORD API

Here I am trying to search for text in context.document.body. I have a loop like this:

var searchList = ['foo','bar'];
while(i < searchList.length){
 Word.run(function(context) {
   var searchResults = context.document.body.search(searchList[i]);
         context.load(searchResults, 'text');
         context.trackedObjects.add(searchResults);
         return context.sync().then(function() {

It is not working.

Upvotes: 1

Views: 502

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33122

You haven't provided enough code to give a precise answer but this is the code that does what you've described:

// Constuct your word array
let searchList = ['foo', 'bar'];

// Iterate through the word list
searchList.forEach(function (searchWord) {
    return Word.run(function (context) {

        // Tell word for search for a word
        let searchResult =
            context.document.body.search(searchWord);

        // Load the properties for the result
        context.load(searchResult);

        // Execute the batch
        return context.sync()
            .then(function () {

                // Loop through the results
                searchResult.items.forEach(function (result)
                { 
                    // Write them to the console
                    console.log(result.text);
                });                    
            });
    });
});

Upvotes: 4

Related Questions