Igor Galvão
Igor Galvão

Reputation: 56

Distinct behavior for Word JS-api for local install and browser version on #document.saved on just opened document

I built an extension that checks periodically if document has been changed. This happens using an interval with a function that uses Word JS Api.

According to the documentation:

Indicates whether the changes in the document have been saved. A value of true indicates that the document hasn't changed since it was saved.

Emphasis mine.

A sample code would be:

setInterval(function () { isSaved(); }, 1000 );
export const isSaved  = async () => {
  return Word.run(async context => {
    const thisDocument = context.document;
    context.load(thisDocument, 'saved');

    await context.sync();

    // edited bellow code for reprodutibility
    console.log(thisDocument.saved);
  });
}

When an existing document and doing nothing with it, we can expect the value to be true, as no changes happened. That is exactly what happens when opening it on Word Online. However, on my local Word application, it returns false.

How to make it behave the same? And why would it return false if document has not been edited?

Upvotes: 1

Views: 46

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

One frequent reason for a document to be marked as "dirty" after being opened, but not edited, is the update of connections to outside sources (a linked Excel table or chart, for example) and field codes.

If, for example, the document has a Pagefield in the footer, that will be updated when the document is opened. Even if the value does not change, this is still regarded by the Word UI as a change to the document. Thus, its saved property is set to false.

The reason this isn't seen in Word On-line is because that version does not support the use (inserting, editing or updating) of field codes. Word On-line does not remove field codes present in the document and may display the calculated field code result at the time the document was last saved. But since Word On-line does not update field codes the saved property will be truein that environment when it is not in Word for Windows or for the Mac.

The only way the value could be the same for all environments would be to remove any content that's updated when Word opens a document. Field codes can be "locked", but not using the Office JS APIs, which do not support field codes (ironically, because they're not available in Word On-line).

Upvotes: 2

Related Questions