Outlook VBA - 每半小时运行一次代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12257985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Outlook VBA - Run a code every half an hour
提问by Tejas
I want run a specific code in outlook(VBA) every half an hour.
我想每半小时在 Outlook(VBA) 中运行一个特定的代码。
Also the outlook user should not get disturbed when the code runs. It should run in back-end only.
此外,代码运行时不应打扰 Outlook 用户。它应该只在后端运行。
There is an event called Application_Reminder
. It runs when a at each occurrence of reminder in outlook. But this still involves user interaction. I want a complete back end procedure.
有一个事件叫做Application_Reminder
。它在 Outlook 中每次出现提醒时运行。但这仍然涉及用户交互。我想要一个完整的后端程序。
回答by niton
http://www.outlookcode.com/threads.aspx?forumid=2&messageid=7964
http://www.outlookcode.com/threads.aspx?forumid=2&messageid=7964
Place the following code in the ThisOutlookSession module (Tools->Macros->VB Editor):
将以下代码放入 ThisOutlookSession 模块(Tools->Macros->VB Editor):
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
Place the following code in an new VBA module
将以下代码放入新的 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
Key points:
关键点:
1) This timer function does not require that a particular window is open; it works in the background
1)这个定时器功能不需要打开特定的窗口;它在后台工作
2) If you don't deactivate the timer when the application closes it will likely crash
2)如果您在应用程序关闭时不停用计时器,它可能会崩溃
3) The example shows the timer being activated at startup, but it can just as easily be called by a different event
3) 该示例显示了在启动时激活的计时器,但它也可以很容易地被不同的事件调用
4) If you don't see the msgbox indicating that the timer was activated upon startup, your macro security is set too high
4) 如果在启动时没有看到指示计时器已激活的 msgbox,则您的宏安全设置太高
5) To have the timer deactivate after one iteration of the time interval add: If TimerID <> 0 Then Call DeactivateTimer after the msgbox statement in sub TriggerTimer
5) 要在时间间隔的一次迭代后停用计时器,请添加:如果 TimerID <> 0 然后在子 TriggerTimer 中的 msgbox 语句之后调用 DeactivateTimer
Someone else suggested
别人建议
"one point to note, if you don't check if TimerID is the same as idevent in the TriggerTimer, you get every so often, and not the time you asked for."
“有一点要注意,如果您不检查 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
回答by David Bollman
For Win64, I needed to change it to this:
对于 Win64,我需要将其更改为:
Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongLong, ByVal nIDEvent As LongLong, ByVal uElapse As LongLong, ByVal lpTimerfunc As LongLong) As LongLong
Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongLong, ByVal nIDEvent As LongLong) As LongLong
Public TimerID As LongLong 'Need a timer ID to eventually turn off the timer. If the timer ID <> 0 then the timer is running
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
Public Sub DeactivateTimer()
Dim lSuccess As LongLong
lSuccess = KillTimer(0, TimerID)
If lSuccess = 0 Then
MsgBox "The timer failed to deactivate."
Else
TimerID = 0
End If
End Sub
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
回答by TuanLaDaPoet
Correct for upper answer for 64-bit:
更正 64 位的上层答案:
Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongLong, ByVal nIDEvent As LongLong, ByVal uElapse As LongLong, ByVal lpTimerfunc As LongLong) As LongLong
Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongLong, ByVal nIDEvent As LongLong) As LongLong
Public TimerID As LongLong 'Need a timer ID to eventually turn off the timer. If the timer ID <> 0 then the timer is running
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
Public Sub DeactivateTimer()
Dim lSuccess As LongLong '<~ Corrected here
lSuccess = KillTimer(0, TimerID)
If lSuccess = 0 Then
MsgBox "The timer failed to deactivate."
Else
TimerID = 0
End If
End Sub
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