How to turn Out of Office on automatically when Outlook is closed?

I REALLY tried to make this work for you, but I learned that Outlook 2010 no longer supports CDO 1.2.1, and not being a programmer, I did not have the deep knowledge to code it another way. Although unsupported and not recommended by Microsoft (why do they mention it then??), it is possible to install CDO if you have Outlook 2007, before you upgrade to Outlook 2010.

http://support.microsoft.com/kb/2028411

I am going to post how to do this in Outlook 2003/2007 in case anyone happens upon this. I just tested this. I will also post the other steps for Outlook 2010 ASSUMING YOU CAN FIX THE CODE.

For Outlook 2003/2007

  1. For Outlook 2007 only, you must install CDO or the code will fail: http://www.microsoft.com/downloads/en/details.aspx?familyid=2714320d-c997-4de1-986f-24f081725d36&displaylang=en

  2. Assuming your company's group policy does not override this, change the security in ToolsMacrosSecurity to No Security Check for macros.

  3. Go to ToolsMacrosVisual Basic Editor.

  4. Click on the Visual Basic icon, and hit F2 to open the objects browser.

  5. In the new project in the left pane expand it until you see ThisOutlookSession and double-click it.

  6. Cut and paste the following code into the code window that just opened and save it:

    Private Sub Application_Quit()
        Dim objMAPISession As Object
        Set objReminders = Nothing
        If MsgBox("Would you like to turn the Out of Office Assistant on?", vbYesNo, "Activate Out of Office Assistant") = vbYes Then
            Set objMAPISession = CreateObject("MAPI.Session")
            objMAPISession.Logon , , True, False
            objMAPISession.OutOfOffice = True
            objMAPISession.Logoff
        End If
        Set objMAPISession = Nothing
    End Sub
    
  7. Close and open Outlook.

  8. It will give you a message about macros. Enable them.

For Outlook 2010

If you can fix the code, here are the steps for Outlook 2010. I include them because the locations of many items have changed, and could be hard to find. In the current code, I also point out the step that fails.

  1. Assuming your company's group policy does not override this, change the security in FileOptionsTrust CenterTrust Center SettingsMacro Settings to Enable all macros.

  2. Start by enabling the Developer tab in FileOptionsCustomize Ribbon, check Developer in the right column.

  3. Click on the Visual Basic icon, and hit F2 to open the objects browser.

  4. Go to Classes (left column) → ThisOutlookSession and double-click it.

  5. Cut and paste the following code into the code window that just opened and save it:

    Private Sub Application_Quit()
        Dim objMAPISession As Object
        Set objReminders = Nothing
        If MsgBox("Would you like to turn the Out of Office Assistant on?", vbYesNo, "Activate Out of Office Assistant") = vbYes Then
            Set objMAPISession = CreateObject("MAPI.Session") THIS IS THE STEP THAT FAILS
            objMAPISession.Logon , , True, False
            objMAPISession.OutOfOffice = True
            objMAPISession.Logoff
        End If
        Set objMAPISession = Nothing
    End Sub
    
  6. Close and open Outlook.

  7. Go back to the Developer tab → Macros icon. It will give you a message about macros. Enable them.


Another way around this (as I don't believe you can change the date/time parameters for the Out Of Office Assistant via VBA) is to perform the following steps:

  1. Create a rule in "Rules and Alerts" for when a message arrives and have that rule auto-reply to every email where you are in the "To" part of the email.
  2. Set the rule to respond using "have server reply using a specific message"
  3. Fill in the "specific message" with a standard template response (rough example: Dear emailer, I am currently of the office. Working hours are 8am to 5pm. I will respond promptly on my return. Yours Sincerely, <-insert signature->)
  4. Name the rule 'HomeTime' (or something meaningful - this will be needed for the vba code below)
  5. Change your VBA macro security in Outlook to enable the running of macros appropriately
  6. Include the following code which runs during Application_Start and Application_Quit to enable/disable the rule:
Option Explicit

Private Sub Application_Quit()
   SetRuleEnabled True
End Sub

Private Sub Application_Startup()
   SetRuleEnabled False
End Sub

Private Sub SetRuleEnabled(ByVal bEnable As Boolean)
   Dim oSession    As Outlook.NameSpace
   Dim oRule       As Outlook.Rule
   Dim oRules      As Outlook.Rules
   Dim oPA         As Outlook.PropertyAccessor

   Set oSession = Application.Session
   Set oRules = oSession.DefaultStore.GetRules()
   Set oPA = oSession.DefaultStore.PropertyAccessor

   '*** If the Out-Of-Office is already on (eg. holidays, sick leave etc.) 
   '*** then it might be best to force this rule permanently off
   If oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x661D000B") Then
      bEnable = False
   End If

   For Each oRule In oRules
      If oRule.Name = "HomeTime" Then
         oRule.Enabled = bEnable
         oRules.Save
         Exit For
      End If
   Next

End Sub

1 issue to keep in mind is that this response will fire each time for each subsequent reply. Unlike the Out-Of-Office assistant that ignores subsequent replies.

PS. With the Out of Office check in the VBA above, you will need reference to the CDO library. If you don't want to check for Out-Of-Office being on, then you won't need CDO.


Awesome! Thanks KCotreau. I got it working on MS Outlook 2003. Below is the code snippet - I just wrapped it wihtin the pre and code to not have it all in a single line.

Private Sub Application_Quit() 
Dim objMAPISession As Object
Set objReminders = Nothing
If MsgBox("Would you like to turn the Out of Office Assistant on?", vbYesNo, "Activate Out of Office Assistant") = vbYes Then
    Set objMAPISession = CreateObject("MAPI.Session")
    objMAPISession.Logon , , True, False
    objMAPISession.OutOfOffice = True
    objMAPISession.Logoff
End If
Set objMAPISession = Nothing
End Sub

This is nice, but is it possible to have it actually run at a future day/time? That will be a real plus, because if you have a vacation planned and forget to turn OOF, it will automatically kick in without human intervention...eh?