Word Addin high CPU after finishing task. Troubleshoot

lars wissler 26 Reputation points
2021-04-22T09:37:53.647+00:00

UPDATE at the bottom.

I have written a word plugin that automatically replaces text within input fields. In short the code is something like this:

 const textItemRange = textItem.getRange(Word.InsertLocation.end);
 const contentControl = textItemRange.insertContentControl();
 textItem.insertText('', Word.InsertLocation.replace);
 contentControl.insertText(`${contentControlValue}`, Word.InsertLocation.start);
 await context.sync();
 storeContentControlReferences();

That is all working fine. However after the process finished, Word is stuck on maximum one-core CPU load for minutes up to half an hour. I assume Word is doing some sort of cleanup/storing duty and something left over by the addin takes a lot of cleaning up. But I am a bit out of ideas how to approach this.

The problem increases with larger files but not greatly so. I have tried working with debugger to look inside what Word is doing generating Performance and Memory snapshots. But I could not find anything out of the ordinary. Makes sense, the debugger is just for the Addin and that has stopped its tasked.

So really the question: How do I find out what Word is calculating like crazy after my Addin stopped running? Help would be much appreciated. My Word is Word 365, Version 2103.

UPDATE

Thanks to Abid's insightful question it turns out, a majority of the post processing CPU consumption is triggered during the storage phase. There is nothing special going on there, so that did not cross my mind. I just store the reference between my internal key, the content control value and the content control ids to reuse after saving and reopening the document. I have a few hundred keys and up to 1500 content controls, so maybe that is overload? Is there a more efficient or Word friendly way of storing this data? The complete function:

private createOrUpdateCustomProperties(normalizedData: Array<{ [key: string]: string; }>): Promise<void> {
    return Word.run(async (context) => {
        normalizedData.forEach(async newProp => {

           //create object with new value
            let updateObj = { value: newProp.value };

          //get content control ids. It is an array of numbers
            let ids;
            for (const listItem of this.linkListToCustomProperty) {
              if (listItem.varName === newProp.key) {
                ids = listItem.ccIds;
                break;
              }

            if (typeof ids !== 'undefined') {

              //add ids to update object
              updateObj['ccIds'] = ids;

               //set custom property to the update object
              context.document.properties.customProperties.add(newProp.key, JSON.stringify(updateObj));
            } 

         //sync
              await context.sync();

          }
        });

        context.document.properties.load('customProperties');
       await context.sync();
    });
  }
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
446 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
1,964 questions
{count} votes