NatB-6501 avatar image
0 Votes"
NatB-6501 asked ElizabethSamuelMSFT-6119 commented

Office Add-in hangs on context.sync() when using wildcards in search

I'm trying to write an office plugin to find all telephone numbers and highlight them. I used the code template from the official documentation to write the following search with wildcards:

 async function checkForText() {
         // Run a batch operation against the Word object model.
         await (context) => {
             // Queue a command to search the document with a wildcard
             // for any string of characters that starts with 'to' and ends with 'n'.
             const searchResults ='[0-9]@-', { matchWildcards: true });
             // Queue a command to load the font property values.
             // 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 = 'pink';
                 searchResults.items[i].font.bold = true;
             // Synchronize the document state.
             await context.sync();

The only line that I have modified is the search expression.

If I run this code on a single line of text it seems to work fine, but if I run it anything more the plugin hangs (forever) at the line await context.sync();. Weirdly, using just the wildcard expression '[0-9]@' finds all sequences of numbers in a large document with no issue. I've checked and '[0-9]@ ' with any character behind it hangs if there is a match to be found. If there's no match, it doesn't hang.

Any idea would be greatly appreciated!

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@NatB-6501 Can you confirm which `await context.sync();' statement is hitting this issue? Is it the first one (line 13)?

I tried this example and it worked ok - it was able to find and highlight the matches. However, maybe my sample file wasn't big enough.

Can you try searching the document using the Word Navigation UI directly (you may need to update the Options to use wildcards)? Does your wildcard search work well there?

0 Votes 0 ·

0 Answers