[UPDATE] Detect Auto-Save Multiple Versions

Giving credit where it is due is important and I thought the code Michael Zlatkovsky provided me after reading my previous post was important enough to add to my blog.

In my previous post, I pointed to a new property in Word 2013 that you can use to access whether a file is in the Auto Save or a regular save event call. You can technically write your code for Office 2010 in VS2010 and VSTO4/.NET4 and still access this property fairly easily, see the code below with Michaels comments.

Additionally, I added the Office 2007/2010 code in there as well, so it is a complete solution for Office 2007 / 2010 and 2013. Thanks Michael!

int version = int.Parse(Application.Version
        .Substring(0, Application.Version.IndexOf(".")));
if (version >= 15)
{
    // By using “dynamic”, you can get access to 
    // the IsInAutosave API even if you’re compiling 
    // against the Office 2010 PIAs. But then when 
    // the published add-in is run under Office 2013,
    // the IsInAutosave behavior is able to execute 
    // properly.
    dynamic dynamicDoc = Doc;
    if (dynamicDoc.IsInAutosave)
    {
        MessageBox.Show("Is Office 2013 autosave");
    }
    else
    {
        MessageBox.Show("If Office 2013 Normal save");
    }
}
else
{
    // This is the Office 2007 / Office 2010 logic
    object oBasic = Application.WordBasic;
    // this is where we invoke the object and
    // get the property. But we get an "object" 
    // back so be careful casting it.
    object fIsAutoSave =
         oBasic.GetType().InvokeMember(
             "IsAutosaveEvent",
             BindingFlags.GetProperty,
             null, oBasic, null);

    if (int.Parse(fIsAutoSave.ToString()) == 1)
    {
        MessageBox.Show("Is Office 2007/2010 AutoSave");
    }
    else
    {
        MessageBox.Show("Is Office 2007/2010 Normal save");
    }
}

Leave a Reply