office365office-jsoffice-addinsword-addinsword-web-addins

How to use the Word JS API to search for text with tracked changes enabled


I have a Microsoft Word document with tracked changes enabled, which I am trying to search for a string of text. I want to search for what the text would be if tracked changes were accepted. I do this using the Office JS API:

await Word.run(async (context) => {
  const searchString = "This is text with tracked changes accepted.";

  const searchRange =
    context.document.body.search(searchString, {
      ignoreSpace: true,
      matchCase: true,
      ignorePunct: false,
      matchWildcards: false,
    });

  searchRange.select();
})

This is what the text looks like in the Word document

However, this code does not find the range because the word "the" is still in the Word document text even though it has been deleted with tracked changes turned on. The search bar within the Microsoft Word app also does not find it.

I am wondering how I could find this text without modifying the document in any way (for example, by accepting tracked changes). It would be nice if there was a workaround where I could accomplish something like the ignoreSpace search option, but instead for ignoring tracked changes.


Solution

  • There is no way to exclude tracked changes until they are accepted (made). But you may consider using wildcards in your search instead of searching for the direct comparison. The matchWildcards parameter indicates whether the search will be performed using special search operators. Corresponds to the Use wildcards checkbox in the Find and Replace dialog box in Word. You can read more about that in the Use search options in your Word add-in to find text article.

    Note, you can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team go through the planning process, see https://aka.ms/M365dev-suggestions .