Auto Recovery Save (AutoSave) fires off the DocumentBeforeSave event in Word


As an Office Development Advisor for Microsoft Premier Field Engineering, I work with a lot of different customers. A lot! However, one item I come across almost every single time, and one I have dealt with as an Escalation Engineer in Product Support Services was that of the dreaded Auto Save Issue. Confused smile

This is the one where Auto Save fires off the Before Save event and there is no way to distinguish if it was a user initiated save or one that came from the built in and timed Auto Save.

Well, while working with a rather large customer last year, we were able to push for and get a small design change to Word that is nearly 15 years in the waiting. If you have Office 2007, you are already in luck. See the following article:

Macros in Office Word 2007 cannot differentiate between "Auto Recovery Save" and "Manual Save" in the DocumentBeforeSave event

(http://support.microsoft.com/kb/2193786)

NOTE: This fix however, is not going to be available in Office 2010 until Service Pack 1 releases (sometime this summer).

Now you VSTO folks don’t get all happy just yet. There is a real catch for you in C#. First sign is that you will notice the article does not have any source code for you. Surprised smile WHAT!!!!

Well, as it turns out thee is there is a small hitch. The design change places the IsAutosaveEvent flag off the WordBasic object. Yes, you read that correctly… the WORD BASIC object. Really!!!! Sick smile

And if you have ever tried to get a listing of properties and methods off the WordBasic object in VSTO/C#, you would quickly realize… there are none defined in the Office PIA’s. Oops. Disappointed smile

However, where there is a will, there is a way. Winking smile

And now, just for you C# folks, here is how you access that confounded flag:

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 AutoSave");
else
    MessageBox.Show("Is regular save");

 

I did not say it was pretty, but what you are doing here is bypassing the PIA’s and directly invoking the object itself. And it works. And it is safe – you just have to be careful with your casting.

So, there you have it. Happy coding!!! Open-mouthed smile

11 thoughts on “Auto Recovery Save (AutoSave) fires off the DocumentBeforeSave event in Word

  1. Very nice info from your side! I was looking for something like this since days.. thanks a lot!!!

  2. Hi David, this is a great article. However, I’ve tried this approach in C# and also in VB but in both cases i’m receiving errors indicating that the WordBasic object doesn’t have any property named IsAutosaveEvent. Any idea why this could happen? Thanks a lot.

      • If you have Word 2007 or Word 2010, fully updated SP1+, the property should be there. If you open the Visual Basic Editor (ALT+F11) from Word and then in the Immediate Window (CTRL+G), type the following command, does this work?

        ? Application.WordBasic.IsAutosaveEvent

        Should return a 0 (returns a 1 when in the event itself). If this does not work, either Word is not fully updated on your system or maybe it is not available in the addition you have: Pro? Pro Plus? Or Student and Home?

    • No. WordBasic is an old remnant from Word for Windows 6.0 (and older) (pre-Windows 95) and there is no similar properties for Excel and PowerPoint. But Excel and PowerPoint do not have this problem where AutoSave also calls the Save event, so such properties are not needed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s