views:

501

answers:

1

Hello

Does anybody know how to programmatically enable/disable the actual state of the out-of-office auto-responder in Outlook 2007?

Already searched the object browser in VS 2008 and found the enumeration Microsoft.Office.Interop.Outlook.OlBusyStatus but i didn't find any class or anything else using this.

Any idea is appreciated, thanks and regards

+3  A: 

UPDATE: Updated the code below using sample code adapted from this blog post which will work better in a wider variety of Outlook installations (e.g. ones using both Exchange and PST or accessing multiple Exchange mailboxes).

Here's code which worked for me on Outlook 2007, to set the OOF status from an external (to Outlook) EXE:

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.NameSpace ns = app.Session;
foreach (Microsoft.Office.Interop.Outlook.Store store in ns.Stores)
{
    if (store.ExchangeStoreType == Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olPrimaryExchangeMailbox)
    {
        store.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x661D000B", true); // false to turn off OOF
        break;
    }
}

Make sure you're not running that code as Administrator and outlook as non-Administrator-- otherwise you may get a security-related error on Vista.

Note that it will pop up security dialogs inside Outlook to ensure the user is OK with you accessing the Outlook object model. This is normal when outlook object model is accessed from an external EXE.

If, however, you're accessing the object model from an add-in, the code above isn't fully correct: instead of creating a new Outlook.Application object via the constructor, you you need to get a reference to the trusted Outlook.Application object from inside your add-in, like this:

Microsoft.Office.Interop.Outlook.NameSpace ns = this.Application.Session;
foreach (Microsoft.Office.Interop.Outlook.Store store in ns.Stores)
{
    if (store.ExchangeStoreType == Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olPrimaryExchangeMailbox)
    {
        store.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x661D000B", true); // false to turn off OOF
        break;
    }
}

BTW, there's a good MSDN article on security for add-ins, which may be useful if you run into security dialogs or errors.

Justin Grant
looks good. Will try it in the office on monday, thanks already now.
Atmocreations
cool. one caveat to watch out for: I believe that the code above is apparently Outlook 2007-specific and requires the latest CDO (1.21 I think) to be present. If this is your dev machine, then it's a non-issue, but if you're bundling this into an app to be installed on other users' desktops, let me know and I can add more info to my answer about what you'll need to do.
Justin Grant
That's the exception i'm receiving:`System.UnauthorizedAccessException was unhandled by user code` on your example and even if I try to `ns.Stores[1].PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x661D000B")`
Atmocreations
Are you getting this exception from code running inside an addin or in a separate app? And what OS version are you on?
Justin Grant
sry for the missing details. I'm trying to create an add-in and I'm on Win XP SP3, bound to a domain using MS Exchange.
Atmocreations
OK, I updated the answer with the code that should work for an add-in. Let me know if this works, and if not we can iterate.
Justin Grant
Thanks. Still the same error, a description is here http://office2007.pastebin.com/m74958d41 . The main error means (translated from german) "This action is not supported by the http://schemas.microsoft.com/mapi/proptag/0x661D000B-property" any idea?
Atmocreations
Check out the new code above-- I wasn't checking each store to make sure it was an exchange mailbox. lack of this check could, in theory, cause the error you were seeing. If you're still seeing the error, let me know and we can iterate (again!) ;-)
Justin Grant
ty. i'll be able to try on thursday. by the way i would like to know where you got the property-number from. the name in general is well-known but how did you know about 0x661D000B?
Atmocreations
I got the hex number from this thread: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/e935c4ec-0b3c-4230-9303-0dd69eb2ce2a
Justin Grant
many thanks! i have tested now it works! but i had to replace `ns.Stores[1].PropertyAccessor.SetProperty(..., true);` by `store.PropertyAccessor.SetProperty(...);`
Atmocreations
hah, glad to help get this working! good catch on the typo-- I fixed the sample above.
Justin Grant