<

Automating Out of Office in Outlook

Published on
48,055 Points
36,555 Views
15 Endorsements
Last Modified:
Awarded
Community Pick
Outlook’s “Out of Office” (OOO) feature is a wonderful tool if your mailbox is hosted on Exchange.  When it’s turned on, your Exchange server sends a reply to anyone who emails you, letting them know that you are out of the office.    

There’s the rub, though.  It only works if you remember to turn it on before exiting Outlook and leaving your office.  I can’t begin to count the number of times that I’ve been on my way out of town only to remember that I forgot to turn on OOO.  Fortunately I have a mobile device that allows me to turn it on remotely.  There are other ways to turn OOO on remotely (e.g. Outlook Web Access, a notebook and a VPN connection), but wouldn’t it be great if Outlook included a feature that turned it on automatically so you wouldn’t have to remember?  

One of the reasons that I’m an Outlook fan is its built-in support for scripting.  Scripting allows me to add a huge amount of functionality that Outlook doesn’t have out of the box.  And while I can’t add every feature I’d like Outlook to have, scripting allows me to add most of them.  This is one.  With a very simple bit of scripting I can automatically turn on OOO every time I close Outlook and turn it off again each time I launch Outlook.  

This works well for me because Outlook is always open when I’m in the office.  The only time it’s closed is when I leave the office.  I think of OOO less as an indicator of whether I’m physically in the office and more as a means of letting anyone who emails me know whether I’m available to respond to their message.  I realize that not everyone views it this way and that some folks won’t want to activate OOO each time they close Outlook.  This article is intended for those of you who do want OOO to kick in each time you close Outlook.

As I noted above, the code for this is very simple.  It works by monitoring the event that fires as Outlook begins its shutdown process.  By trapping that event, we can run the code that turns on the OOO feature.  Similarly, by monitoring the event that’s triggered each time Outlook launches we can run code that turns off the OOO notification.  Here’s the code for doing this.

Outlook 2007/2010

Follow these instructions to add the code to Outlook 2007/2010.

  1.  Start Outlook.
  2.  Click Tools > Macro > Visual Basic Editor.
  3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession.
  4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's
       VB Editor window.
  5.  Edit the code as needed.  I included comment lines wherever something needs to or can change.
  6.  Click the diskette icon on the toolbar to save the changes.
  7.  Close the VB Editor.
  8.  Click Tools > Trust Center.
  9.  Click Macro Security.
10.  Set Macro Security to "Warnings for all macros".
11.  Click OK.
12.  Close Outlook.
13.  Start Outlook.  Outlook will display a dialog-box warning that ThisOutlookSession contains macros
       and asking if you want to allow them to run.  Say yes.

Private Sub Application_Quit()
    OutOfOffice True
End Sub

Private Sub Application_Startup()
    'Remove this subroutine if you do not want to turn OOF off automatically.'
    OutOfOffice False
End Sub

Sub OutOfOffice(bolState As Boolean)
    Const PR_OOF_STATE = "http://schemas.microsoft.com/mapi/proptag/0x661D000B"
    Dim olkIS As Outlook.Store, olkPA As Outlook.PropertyAccessor
    For Each olkIS In Session.Stores
        If olkIS.ExchangeStoreType = olPrimaryExchangeMailbox Then
            Set olkPA = olkIS.PropertyAccessor
            olkPA.SetProperty PR_OOF_STATE, bolState
        End If
    Next
    Set olkIS = Nothing
    Set olkPA = Nothing
End Sub

Open in new window


Note: I have not tested the code with Outlook 2010.

Outlook 2003 and Earlier

Follow these instructions to add the code to Outlook 2003 and earlier.

  1.  Start Outlook
  2.  Click Tools > Macro > Visual Basic Editor
  3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
  4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of
  5.  Outlook's VB Editor window
  6.  Edit the code as needed.  I included comment lines wherever something needs to or can change
  7.  Click the diskette icon on the toolbar to save the changes
  8.  Close the VB Editor
  9.  Click Tools > Macro > Security
10.  Set the Security Level to Medium
11.  Close Outlook
12.  Start Outlook
13.  Outlook will display a dialog-box warning that ThisOutlookSession contains macros
       and asking if you want to allow them to run.  Say yes.

Private Sub Application_Quit()
    OutOfOffice True
End Sub

Private Sub Application_Startup()
    'Remove this subroutine if you do not want to turn OOF off automatically.'
    OutOfOffice False
End Sub

Sub OutOfOffice(bolState As Boolean)
    Dim mapSession As Object
    Set mapSession = CreateObject("MAPI.Session")
    With mapSession
        .Logon , , False, False, 0
        .OutOfOffice = bolState
        .Logoff
    End With
    Set mapSession = Nothing
End Sub

Open in new window

Note: CDO (Collaboration Data Objects) must be installed on the computer.  If you discover that CDO is not installed, then you can download it from this Microsoft page.
With this code in place, OOO will activate each time you close Outlook and will deactivate each time you launch Outlook.  Keep in mind that this solution will ONLY work if your mail is hosted on an Exchange server.  It will not work if your mailbox is hosted on a POP server.  I should also point out that the code only works from the full version of Outlook.  Outlook Web Access does not support scripts.

Enhancements

The code, as is, does not prompt for a message.  It simply turns OOO on or off.  You can use a generic message such as “I’m out of the office right now.  I’ll respond to your email when I return.”  If you want to be prompted for a message, then you can add that feature.
You may not want OOO to activate every time you close Outlook.  Perhaps you only want it turn on if you close Outlook after a certain time of the day.  For example, if it’s after 3:00 PM turn it on otherwise do nothing.  You can do that by adding an IF … THEN statement before the FOR … NEXT loop.
15
Author:David Lee
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free