Not search the part that was previously searched Officejs

39 Views Asked by At

"I have something with me." Currently I am using context.document.body.search('something', { matchCase: true, matchWholeWord: true }).load('items/NoPropertiesNeeded'); to search 'something' and the 'something' gets loaded in the taskpane.

"I have something with me. something is bothering me" Now I dont want to search the 'something' on first paragraph that was already searched and loaded in taskpane. How can I do so?

1

There are 1 best solutions below

0
On

The Word.SearchOptions class doesn't provide anything for that. You need to iterate over search results like shown below and excluded unwanted results from the view or just skip them:

// Run a batch operation against the Word object model.
await Word.run(async (context) => {

    // Queue a command to search the document and ignore punctuation.
    const searchResults = context.document.body.search('video you', {ignorePunct: true});

    // Queue a command to load the font property values.
    searchResults.load('font');

    // Synchronize the document state.
    await context.sync();
    console.log('Found count: ' + searchResults.items.length);

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

    // Synchronize the document state.
    await context.sync();
});

See Use search options in your Word add-in to find text for more information.

Note, you can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team goes through the planning process.