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.
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
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.
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!!!!
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.
However, where there is a will, there is a way.
And now, just for you C# folks, here is how you access that confounded flag:
// 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!!!