Friday 14 October 2016

Running Outlook Macros on a Schedule

Outlook doesn't have a timer function but you can use Appointment or Task Reminders to trigger macros. Set up an Application_Reminder macro that will do something when a reminder fires. To limit it to running when specific reminders fire, use an If statement to look for words in the subject or a specific category.
If you want the macro to fire a specified time after you restart Outlook, use an Application_Startup macro to create the appointment.
Create an appointment reminder to trigger a macro
If you need to rerun the process kicked off by the reminder, run CreateAppointment macro from the VBA editor at any time.
VBA
62 lines
' The Private subs go in ThisOutlookSession
Private WithEvents olRemind As Outlook.Reminders
Private Sub Application_Startup()
CreateAppointment
End Sub
Private Sub Application_Reminder(ByVal Item As Object)
Set olRemind = Outlook.Reminders
If Item.MessageClass <> "IPM.Appointment" Then
Exit Sub
End If
If Item.Categories <> "Run in 5" Then
Exit Sub
End If
' Call your macro here
MsgBox "It works!"
'Delete Appt from calendar when finished
Item.Delete
' Create another appt to repeat the process
CreateAppointment
End Sub
' dismiss reminder
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
For Each objRem In olRemind
If objRem.Caption = "This Appointment reminder fires in 5" Then
If objRem.IsVisible Then
objRem.Dismiss
Cancel = True
End If
Exit For
End If
Next objRem
End Sub
' Put this macro in a Module
Public Sub CreateAppointment()
Dim objAppointment As Outlook.AppointmentItem
Dim tDate As Date
' Using a 1 min reminder so 6 = reminder fires at 5 min.
tDate = Now() + 6 / 1440
Set objAppointment = Application.CreateItem(olAppointmentItem)
With objAppointment
.Categories = "Run in 5"
.Body = "This Appointment reminder fires in 5"
.Start = tDate
.End = tDate
.Subject = "This Appointment reminder fires in 5"
.ReminderSet = True
.ReminderMinutesBeforeStart = 1
.Save
End With
End Sub

How to use the macro

First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s atTools, Macro Security. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.

To use the macro code in ThisOutlookSession:
  1. Expand Project1 and double click on ThisOutlookSession.
  2. Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
Application_Startup macros run when Outlook starts. If you are using an Application_Startup macro you can test the macro without restarting Outlook by clicking in the first line of the Application_Startup macro then clicking the Run button on the toolbar or pressing F8.
To put the code in a module:
  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

0 comments:

Post a Comment