Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

Automating Out of Office in Outlook

David Lee
CERTIFIED EXPERT
Published:
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
39,452 Views
David Lee
CERTIFIED EXPERT

Comments (24)

CERTIFIED EXPERT
Top Expert 2010

Author

Commented:
Hi, Amanda.

Yes, what you've described is possible.  There are actually two ways to do this.  One, turn OOO on when an appointment begins and turn it off again when an appointment ends.  Second, each time a new message arrives check to see if you're in an appointment.  If you are, then send a pseudo-OOO response (I say pseudo because it wouldn't be an actual OOO response, it would be like an OOO response).  The main difference between the two is that OOO will only respond to each sender once, whereas the second approach will respond to every message they send.  Beyond that, there are limitations on automating OOO without resorting to third-party tools.  That makes the second approach slightly more viable than the first approach.

Commented:
Hi BDF,

I´m using Outlook 2010.

Commented:
Hello there,
I saw the question asked regarding turning on the Out of Office AND somehow checking the "Auto-reply to people outside my organization" checkbox (or the code equivalent of checking the box!)

I didn't see a posted answer, and this would solve my problem in a major way! Some of our team members have varying or different hours/shifts, and I want an auto-reply or Out of Office message sent so the sender knows that their recipient is not in.

Any advice is GREATLY appreciated. Thank you!
-Jeremy
It looks like this website might be able to make this easier http://www.thebusyapp.com
Thank you for this code - it's very useful :-)

I have a couple of possible extensions please?

I would like to be able to change the text of the OOO reply when it is set - is this possible? Preferably slightly different inside & outside my organisation!

Also, I would like to be able to set it so it replies to "My Contacts" "Outside My Organisation" - is that also possible?

Many thanks :-)

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.