UPDATE: The updated code below, using sample code adapted from this blog post , which will work better in a wider variety of Outlook (for example, using Exchange and PST or accessing multiple Exchange mailboxes).
Here is the code that worked for me in Outlook 2007 to set the OOF status from an external (in 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 that you do not use this code as an Administrator and Outlook as a non-Administrator - otherwise you may receive a security-related error in Vista.
Note that security dialogs will appear in Outlook to ensure that the user is in order by accessing the Outlook object model. This is normal when the Outlook object model is accessible from an external EXE.
If, however, you access the object model from an add-in, the code above is not entirely correct: instead of creating a new Outlook.Application object through the constructor, you need to get a link to the trusted Outlook.Application object from your add-in, for example:
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; } }
By the way, there is a good article in the MSDN article on security for add-ons, which can be useful if you run dialogs or security errors.
Justin grant
source share