1

Is there a way to call a function when user click on a contentControl or highlighted text in Office-js?

I am working on add-in for MS word, and I have inserted content controls which will be linked with a list in my add-in panel. When I click on list item from add-in, I am able to highlight the text in word document and also able to focus on it.

Now, I want to do the other way around, when I click the highlighted text in word document, a click event should be triggered, and list should be scrolled to the corresponding list item from add-in and highlight it.

I have tried some solutions from this post: MS Word JavaScript API - event handler for Content Controls

I am using typescript and react in add-in development.

Pooja M
  • 21
  • 1
  • What code have you tried so far? – Eugene Astafiev Dec 28 '22 at 14:24
  • I have tried solution shared in this [post](https://stackoverflow.com/questions/52157240/microsoft-word-javascript-api-event-handler-for-text-selection-in-document). However, handler function is called when user select text in document, and it is not returning any id or selected text which I can use further. – Pooja M Dec 29 '22 at 06:12
  • Have you tried [onSelectionChanged](https://learn.microsoft.com/en-us/javascript/api/word/word.contentcontrol?view=word-js-preview#word-word-contentcontrol-onselectionchanged-member)? – Eugene Astafiev Dec 29 '22 at 10:16

2 Answers2

1

The onSelectionChanged is pretty slow at firing. I found the onSelectionChange to take ~0.5-2 seconds to fire. This proved to be too slow for my use case, so I wrote a routine to ping the document every 250ms to see if the selection has changed. It is not ideal, but it does work. Best of all it should work outside of the preview only version of the API. The only downside is you are pinging the document every 250ms. If you are doing other actions to the document I highly recommend you pause the await context.sync() action in this code until you complete your other actions. Otherwise this code will slow down your other actions.

Please note that I wrote these functions originally in Vue, and converted it to javascript just now. So there might be a small mistake I made in the conversion process.

The first step is to use setInterval to run the function every 250ms:

    async function subscribeToAllEvents() {
      await Word.run(async (context) => {
        var events = setInterval(
          documentSelectionChangedEventCustom,
          250,
        );
      });
    }

The Function will assume that the user has a content control selected. If they don't then an error will be thrown and caught.

    async function documentSelectionChangedEventCustom() {
      await Word.run(async (context) => {
        // Assumes selection is a content control:
        try {
          let document = context.document;
          let selection = document.getSelection();

          const contentControl = selection.parentContentControl;

          contentControl.load('text');
          contentControl.load('tag');

          await context.sync();
          // Error is thrown above if the user doesn't have a content control selected

          // User has a Content Control Selected:
          const contentControlText = contentControl.text;
          const contentControlTag = contentControl.tag;
          await context.sync();

          // If the user does not have a content control selected then the catch will run. 
         // A better way to do this is to only catch the specific error associated with accessing the content control.
        } catch {
          // User Selection is in the Document
        }
      });
    }
0

You may be interested in the onSelectionChanged event which is fired when selection within the content control is changed. Note, this event is provided as in preview and not availble in a production environment yet.

There are no other events for that. 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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks for your response! As per the name, onSelectionChanged looks like an event to be triggered if user select something within content control. I want a click event on content control which will return content control's id/tag. Is it something achievable using onSelectionChanged? Also, could you please let me know when this event will be available for production environment? – Pooja M Dec 29 '22 at 11:12
  • There is no exact timeline when the event goes in production, such news are announced on big events or community calls. – Eugene Astafiev Dec 29 '22 at 12:13