Building custom solutions that extend, automate, and integrate Microsoft 365 apps.
Yes, there is an event in the Outlook add-in API that allows you to detect when the add-in panel is closed. The event is called Office.EventType.DialogEvent.DialogClosed. You can use this event to attach a callback function that is invoked whenever the add-in panel is closed. This callback function allows you to perform any necessary actions, such as saving data or resetting the state of your add-in, before the panel is closed.
You can register the event by calling the Office.context.ui.addEventHandler method, like this:
Copy code Office.context.ui.addEventHandler(Office.EventType.DialogEvent.DialogClosed, myCallbackFunction); In the callback function, you can check the result property of the event object, which will be 'cancel' if the user closed the dialog by clicking the close button, or 'success' if the user completed the action.
Copy code function myCallbackFunction(event) { if (event.result === 'cancel') { // Perform actions when user closes the dialog explicitly } } You can also use the Office.EventType.ItemChanged event, this event triggers whenever the user navigates to another email or calendar item, you can use this event to detect when the user leaves the current email and save any changes before the add-in is closed.
Copy code Office.context.mailbox.addHandlerAsync(Office.EventType.ItemChanged, myCallbackFunction); Please note that this event only works when the add-in is running in an email context (i.e. when the add-in is open in an open email or calendar item)