Search code examples
ms-officeoffice-jsoffice-app

Save before closing a document


I have developed an Office add-in. It contains an editor where users could type and save a text.

I want to realise a feature: 1) One option is, when a user closes a spreadsheet/document, I want to remind them of saving the text on the add-in (by simply clicking on a button of the add-in); 2) another option is, when a user closes a spreadsheet/document, the text on add-in is systematically saved.

In either case, we need to be able to listen to the event when a document is closed, or a browser tab is closed (if it is Office Online).

Does anyone know how to realize this?


Solution

  • There is no close document event API yet. You can make your API request here. The event to intercept document/tab/window close action is not possible consistently because the process itself could end and also JS APIs are asynchronous in nature.

    The Word API offers save API that could be used to save the document. Word also offers document.saved (Boolean) API to check periodically and save the document if that matches the requirement. Excel also offers workbook.saved API (still in preview) but not the save() API.