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.
OlBusyStatus
is forAppointmentItem.BusyStatus
. – Prosit