Timer in outlook

  • Thread starter Thread starter Guest
  • Start date Start date
Am Mon, 5 Dec 2005 09:08:02 -0800 schrieb קובץ:


hello!

I want to add a timer to my userform. how do I do that?

thanks!
Yonina.

Yonina, the MSForms don´t know a timer. You can use an API Timer instead.

Please add a standard module to your project (no class module). Copy the
code into it; you don´t need to change anything.

<modTimer>
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
m_oCallback.Timer
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>


Here comes a sample for how to use the timer. Please copy the code into
"ThisOutlookSession". There´re some usefull comments within the code.

<ThisOutlookSession>
' True means, the timer will be disabled after one event is raised.
' False means, the timer will raise an event again and again _
for the given interval.
Private Const RUN_ONCE As Boolean = True

' Interval for the timer in milliseconds.
Private Const TIMER_INTERVAL As Long = 60000

' This function is necessary and is being called from the timer.
Public Sub Timer()
If RUN_ONCE Then
' True disables the timer
modTimer.DisableTimer
End If

' Insert another code here, which shall be executed _
by the timer events.
' ...
End Sub

' Sample for starting the timer immediately if Outlook starts.
Private Sub Application_Startup()
modTimer.EnableTimer TIMER_INTERVAL, Me
End Sub

' Stops the timer if Outlook is being closed.
' NOTE: This doesn´t work in Outlook 2000!
Private Sub Application_Quit()
modTimer.DisableTimer
End Sub
</ThisOutlookSession>
 
Back
Top