3

Is there a way to fire a function when clicking on a contentControl in Office-js?

I am writing an add-on for MS Word, and I have inserted content controls, that will be associated with a list in my add-in. I need to fire a function when clicking on the content control, so I can scroll to the item in the list and highlight it.

I have found the "Document.ContentControlOnEnter event (Word)" which seems to be what I am looking for, but I cannot understand how to attach an event-listener that listens to this event.

I am writing the plugin in typescript react.

Atonic
  • 509
  • 1
  • 5
  • 14
  • 1
    The Document.ContentControlOnEnter event is part of the VBA APIs, not the Office-js APIs, so it is unclear whether you are making a VBA add-in or a web add-in. Please clarify. – Rick Kirkham Mar 23 '22 at 17:42
  • Thank you! No I intend to write an Office-js API add-in. I was just confused in the documentation. Do you know of any way at all that I can run a function when a user enters or clicks a content control? – Atonic Mar 23 '22 at 22:02
  • Are you looking for this? https://learn.microsoft.com/en-us/javascript/api/word/word.contentcontrol?view=word-js-preview According to this page there are three events: ```onDataChanged```, ```onDeleted``` and ```onSelectionChanged```. Would you be able to use these events to fire your function? – Ninca Tirtil Mar 24 '22 at 12:47
  • Yes, I have also seen that. Two issues, first they are listed as preview and should not be used in production, second I cannot figure out how to listen to those events. I tried listening to thos events in this event handler, but it was not allowed: Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, handler); – Atonic Mar 24 '22 at 13:33

1 Answers1

1

Word web add-ins don't provide anything for that.

UPD: 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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I have seen other add-ins that has this functionality. If I click on a content control the add-in is updated to show the information of the clicked content control. How do others do it if it does not exist? Is this possible if I build my add-in through some other technology? – Atonic Mar 23 '22 at 21:56
  • 1
    You may be interested in the [onSelectionChanged](https://learn.microsoft.com/en-us/javascript/api/word/word.contentcontrol?view=word-js-preview#word-word-contentcontrol-onselectionchanged-member) event. – Eugene Astafiev Dec 28 '22 at 14:23