http://www.outlookcode.com/threads.aspx?forumid=2&messageid=7964
将以下代码放入 ThisOutlookSession 模块(工具->宏->VB 编辑器):
Private Sub Application_Quit()
If TimerID <> 0 Then Call DeactivateTimer 'Turn off timer upon quitting **VERY IMPORTANT**
End Sub
Private Sub Application_Startup()
MsgBox "Activating the Timer."
Call ActivateTimer(1) 'Set timer to go off every 1 minute
End Sub
将以下代码放入新的 VBA 模块中
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerfunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public TimerID As Long 'Need a timer ID to eventually turn off the timer. If the timer ID <> 0 then the timer is running
Public Sub ActivateTimer(ByVal nMinutes As Long)
nMinutes = nMinutes * 1000 * 60 'The SetTimer call accepts milliseconds, so convert to minutes
If TimerID <> 0 Then Call DeactivateTimer 'Check to see if timer is running before call to SetTimer
TimerID = SetTimer(0, 0, nMinutes, AddressOf TriggerTimer)
If TimerID = 0 Then
MsgBox "The timer failed to activate."
End If
End Sub
Public Sub DeactivateTimer()
Dim lSuccess As Long
lSuccess = KillTimer(0, TimerID)
If lSuccess = 0 Then
MsgBox "The timer failed to deactivate."
Else
TimerID = 0
End If
End Sub
Public Sub TriggerTimer(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
MsgBox "The TriggerTimer function has been automatically called!"
End Sub
关键点:
1) 此定时器功能不需要打开特定的窗口;它在后台运行
2) 如果您在应用关闭时不停用计时器,它可能会崩溃
3) 该示例显示了在启动时激活的计时器,但它可以很容易地被不同的事件调用
4) 如果您在启动时没有看到提示计时器已激活的消息框,则说明您的宏安全设置过高
5) 要在时间间隔的一次迭代后停用计时器添加: If TimerID 0 Then Call DeactivateTimer 在 sub TriggerTimer 中的 msgbox 语句之后
别人推荐的
“需要注意的一点,如果你不检查TimerID是否与TriggerTimer中的idevent相同,你会得到每隔一段时间,而不是你要求的时间。”
Public Sub TriggerTimer(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
'keeps calling every X Minutes unless deactivated
If idevent = TimerID Then
MsgBox "The TriggerTimer function has been automatically called!"
End If
End Sub