API Timer

A timer enables you to call your code at regular intervals.

Last modified: 2006/01/18 | Accessed: 53.011  | #4
◀ Previous sample Next sample ▶


ReplyAll ReplyAll
ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the e-mail.

The Timer Modul

A timer is useful, for instance, if you want to start a function at regular intervals. Another scenario is to uncouple calls. For instance, if you need to clean up a folder at the startup of Outlook, you can do that right away in the Application_Startup event. If you do so, Outlook must wait until your code has finished. This could be annyoing for the user, and since Outlook 2013 your code could be punished. If you instead only start the timer in the Application_Startup event, the code execution immediately returns, and Outlook can continue to startup. Your macro will be called later then.

Click Insert/Module and paste the following code into the new module:

tip  How to add macros to Outlook
Private Declare Function SetTimer Lib "user32.dll" (ByVal hwnd As Long, _
  ByVal nIDEvent As Long, ByVal uElapse As Long, _
  ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32.dll" (ByVal hwnd As Long, _
  ByVal nIDEvent As Long) As Long

Const WM_TIMER = &H113

Private hEvent As Long
Private m_Callback As ThisOutlookSession

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, _
  ByVal wParam As Long, ByVal lParam As Long _
  If uMsg = WM_TIMER Then
  End If
End Sub

Public Function EnableTimer(ByVal Interval As Long, Callback As ThisOutlookSession) As Boolean
  If hEvent <> 0 Then
    Exit Function
  End If
  hEvent = SetTimer(0&, 0&, Interval, AddressOf TimerProc)
  Set m_Callback = Callback
  EnableTimer = CBool(hEvent)
End Function

Public Function DisableTimer()
  If hEvent = 0 Then
    Exit Function
  End If
  KillTimer 0&, hEvent
  hEvent = 0
End Function
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

Sample Call

The folowing is a sample for how to start the timer when Outlook starts. The interval is set to 60,000 ms, that is the TimerEvent procedure will be called after 60 seconds. In that event procedure you can disable the timer if you just need that one call. Or keep the timer running if you need regular calls every 60 seconds.

In any case, before Outlook terminates at the latest you must disable the timer else Outlook would crash. Calling DisableTimer from within Application_Quit as shown is sufficient.

Private Sub Application_Startup()
  EnableTimer 60000, Me
End Sub

Public Sub TimerEvent()
  'disable if you don't need more signals
End Sub

Private Sub Application_Quit()
End Sub
Category-Manager Category-Manager
With Category-Manager you can group your Outlook categories, share them with other users, filter a folder by category, automatically categorize new emails, and more. You can use the Addin even for IMAP.
email  Send a message