StartDownloadsServiceSamplesWorkshopsContact DeutschEnglish
Awarded by
Microsoft since 2005:
mvp logo
VBOffice Info
Privacy Policy
API Timer
Author: Michael BauerHomepage
Date: 18.01.2006Accessed: 33538

A timer is useful if you want to start a function in a short interval. Because there's no Timer control in VBA available you have to use the Win32 API instead.

This sample shows such an API timer, and how to receive its event: Create a standard module, here called 'modTimer' and copy the code into it, and create a public method called 'Timer' in 'ThisOutlookSession'. That method will be called by the timer. The timer is enabled in Application_Startup, that is when Outlook starts, with an interval of 60000 milliseconds (= 1 minute).

Please note: you must disable the timer before Outlook quits!

Private Sub Application_Startup()
  EnableTimer 60000, Me
End Sub

Public Sub Timer()
  ' Called from the timer
End Sub

Private Sub Application_Quit()
End Sub

' <Modul: modTimer.bas>
Option Explicit
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_oCallback As Object

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 msInterval As Long, oCallback As Object) As Boolean
  If hEvent <> 0 Then
    Exit Function
  End If
  hEvent = SetTimer(0&, 0&, msInterval, AddressOf TimerProc)
  Set m_oCallback = oCallback
  EnableTimer = CBool(hEvent)
End Function

Public Function DisableTimer()
  If hEvent = 0 Then
    Exit Function
  End If
  KillTimer 0&, hEvent
  hEvent = 0
End Function
' <modTimer.bas>

ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the ... [more]


Access the master category list in the blink of an eye, share your categories in a network, get a reminder service, and ... [more]


SAM automatically sets the sender, signature, and folder for sent items, for instance based on the recipient ... [more]


OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or ... [more]