Search code examples
c#ms-wordvstooffice-addinsword-addins

Differentiate Word manual save from auto save and auto recovery save


I want to run some operations only when user does a Word manual save within the Application_DocumentBeforeSave event in my VSTO project (supports Word 2013 and up).

In order to do that, I need to differentiate Word manual save from auto save and auto recovery save. enter image description here

My current code can only identify the Word auto recovery save.

public void Application_DocumentBeforeSave(Word.Document doc, ref bool SaveAsUI, ref bool Cancel)
{
    object oBasic = Application.WordBasic;
    object fIsAutoSave = oBasic.GetType().InvokeMember("IsAutosaveEvent", BindingFlags.GetProperty, null, oBasic, null);
    if (int.Parse(fIsAutoSave.ToString()) == 1)
    {
        //Auto save triggered by word auto recovery
    }
    else
    {
        //Some operations goes in here
    }
}

fIsAutoSave.ToString() returns 1 only when the save is an auto recovery save. For manual save and auto save, it returns 0. Every time SaveAsUI is false too.

I tried several solution from web, but all of them only identifying the auto recovery save of Word. Could somebody please help me with this?

Thank you in advance.


Solution

  • The Word object model doesn't provide anything for that.