Share via

doc.undo() works in Desktop version of Word but fails in the Online version

RahulK 20 Reputation points
2026-01-08T14:31:07.0366667+00:00

Problem Statement : There is a Word document which is being modified by multiple people, not simultaenously though(basically supports co-authoring). This includes all redlines, all comments, content controls, customXMLMappings etc. The requirement is to generate a completely sanitized version of the same document which is devoid of any customXMLMappings, content controls, custom document properties, custom metadata etc.

Current Approach : We are doing the following which is working in Desktop version of Word but is failing in online version.

  1. Loading Metadata: Fetching IDs for Content Controls and Custom XML parts.
  2. Disable "Track Changes": Saving the current changeTrackingMode and turning it off. Doing this ensures that the sanitization steps (like deleting text) aren't recorded as visible "redline" edits.
  3. Unlinking: We remove any XML mapping so the box is no longer "connected" to a data source.
  4. Flattening: We run insertText(cc.text, "Replace") to effectively delete the Content Control "container" but keep the text inside it.
  5. Sync: We invoke ctx.sync(), which sends these commands to the Word application to be executed.
  6. Replacing: We replace the content of each XML part with an empty tag (<empty/>).
  7. Deletion: We delete the entire XML part.
  8. Error Handling: Usage of a try/catch block inside the loop. Based on what we found on the web, some XML parts are "built-in" or protected by Word; if the code tries to delete one of these, it will fail. The catch block allows the script to skip protected parts and keep moving.
  9. Accepting Revisions: Invocation of acceptAllRevisions(), thereby putting all "pending" changes into the final text.
  10. Custom Properties Deletion: We delete all Custom Properties.
  11. Storing the "Clean" State: We call getInternalDocumentBase64() to save the currently sanitized state into a variable (which is a Base64 string).
  12. Revert Everything: We loop through a doc.undo() command for every sync() that occurred.
  13. Restoration: We restore the original "Track Changes" setting.
  14. Variable Assignment: The document looks exactly like it did before the function started, but the variable cleanBase64 now holds the sanitized version.
  15. Verification: We create a temporary document in memory using the cleanBase64 data and opens it to fetch the clean sanitized version of the document.

Now, step 12 is where the code starts to fail for the online version of Word. We need to undo the changes, but that throws an exception.

Could you please suggest a resolution for this or suggest an alternative that works seamlessly in both desktop as well as online version of Word.

Microsoft 365 and Office | Development | Office JavaScript API
{count} votes

2 answers

Sort by: Most helpful
  1. Austin-H 8,330 Reputation points Microsoft External Staff Moderator
    2026-01-09T01:54:10.7233333+00:00

    Hello RahulK
    Thank you for posting question in Microsoft Q&A Forum.
    According to this issue, I have found this URL: https://learn.microsoft.com/en-us/javascript/api/requirement-sets/word/word-api-desktop-1.4-requirement-set?view=word-js-preview
    The WordApiDesktop requirement set is a special requirement set that includes features that are only available for Word on Windows and on Mac. APIs in this requirement set are considered to be production APIs for the Word application on Windows and on Mac. WordApiDesktop APIs are considered to be "preview" APIs for other platforms (for example, web) and may not be supported by any of those platforms.
    I also found undo() is displayed on this:
    User's image

    You could check on this by searching in the table below the URL I provided above.

    Based on my understanding, in Word on the web, every user is a part of real time co-authoring session. When you call acceptAllRevisions(), Word on the Web must reconcile this with the "Master" version on the server. To prevent the document from becoming corrupted or out of sync for other users, the web client often clears the local Undo stack. Once the stack is cleared, the undo() command fails because there is no "previous state" to return to.
    Instead of using undo() as it is unreliable in the browser, you could consider performing a sanitization on a "copy" of the document data. Instead of changing user's live document, you could take a copy then clean it and save the result.
    I also read your step 15 and thought that it should have been the first step of this sanitization process.
    After all, my suggestion should be using 15th step as the first step as the undo() has its own limitation.
    I hope this information is helpful. Please let me know if I misunderstood your point at any time or if you have any further question.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Q&A Assist
    2026-01-08T14:32:24.55+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The issue you're encountering with the doc.undo() method failing in the online version of Word may be due to the partial support for undo functionality in the Office JavaScript APIs for online applications. Unlike the desktop version, where the undo stack can be manipulated more freely, the online version has limitations that can lead to exceptions when trying to undo certain operations, especially after a series of complex changes like the ones you are performing.

    To resolve this, consider the following alternative approaches:

    1. Avoiding Undo: Instead of relying on doc.undo() to revert changes, you can maintain a copy of the document's state before making modifications. This way, you can simply restore the document to its original state by reloading the saved version instead of using undo.
    2. Batch Operations: Try to minimize the number of sync() calls. Group multiple changes together and call ctx.sync() less frequently. This can help reduce the complexity of the undo stack and may prevent exceptions.
    3. Error Handling: Enhance your error handling to manage specific exceptions that may arise from the online version. This could involve checking the type of error thrown and deciding whether to skip the undo operation or take an alternative action.
    4. Simplifying the Process: If possible, simplify the sanitization process by reducing the number of steps or operations that require undo. For example, if certain operations can be combined or eliminated, it may help in avoiding issues with the undo functionality.
    5. Consult Documentation: Keep an eye on the latest updates in the Office JavaScript API documentation, as Microsoft may introduce improvements or changes that could affect how undo operations are handled in the online version.

    By implementing these strategies, you should be able to create a more robust solution that works seamlessly across both desktop and online versions of Word.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.