lukeupup's avatar
lukeupup
Copper Contributor
Dec 15, 2025
Status:
New

[Office.js] [Word] Add functionality to close hidden document (DocumentCreated)

Cloned from https://github.com/OfficeDev/office-js/issues/6339#event-21523882551

Context

We use the DocumentCreated object to do some background operation (like converting html to ooxml). And from the name of the requirement set (WordApiHiddenDocument), I believe this API is designed to run without user's action. But there're two problems:

  1. The user is asked to save a document which he/she haven't seen, which causes big confusion, especially when there're many DocumentCreated objects. BTW, I remember there was no such prompt in earlier version of Word.
  2. From the behavior, we know that the created documents are keeping opened until all Word's processes are terminated. This may cause serious memory leak.

We suggest, there should be a close method on DocumentCreated, just like normal Document class, so that we can close the Document manually.

Steps to reproduce

  1. Open Script Lab
  2. Paste and run the code in the "Code Snippet" Section
  3. In script lab "Run" page, Click "Run" button several times.
  4. Close Word. Note: You need to close all Word windows, not only the current document. On mac, you need also exit Word process by selecting "Quit" on Dock menu.
  5. You can see that Word asked several times to save Document5, 4, ..., 2, which are hidden documents.

     

Code Snippet

   await Word.run(async (context) => {
    const doc = context.application.createDocument();
    await context.sync();
    doc.body.insertText('xxxx', 'Start');
    await context.sync();
    doc.body.clear(); // clear won't change the behavior
    await context.sync();
    doc.untrack(); // untrack won't change the behavior
    await context.sync();
  })

 

No CommentsBe the first to comment