Outlook VBA - 每半小时运行一次代码 [英] Outlook VBA - Run a code every half an hour

查看:142
本文介绍了Outlook VBA - 每半小时运行一次代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每半小时在 Outlook(VBA) 中运行一个特定的代码.

I want run a specific code in outlook(VBA) every half an hour.

此外,代码运行时不应打扰 Outlook 用户.它应该只在后端运行.

Also the outlook user should not get disturbed when the code runs. It should run in back-end only.

有一个名为 Application_Reminder 的事件.它在 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.

推荐答案

http://www.outlookcode.com/threads.aspx?forumid=2&messageid=7964

将以下代码放入ThisOutlookSession模块(工具->宏->VB编辑器):

Place the following code in the ThisOutlookSession module (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

将以下代码放入新的 VBA 模块

Place the following code in an new VBA module

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) 这个定时器功能不需要打开特定的窗口;它在后台运行

1) This timer function does not require that a particular window is open; it works in the background

2) 如果您在应用程序关闭时不停用计时器,它可能会崩溃

2) If you don't deactivate the timer when the application closes it will likely crash

3) 这个例子展示了定时器在启动时被激活,但它也可以很容易地被不同的事件调用

3) The example shows the timer being activated at startup, but it can just as easily be called by a different event

4) 如果您在启动时没有看到指示计时器已激活的 msgbox,则您的宏安全设置过高

4) If you don't see the msgbox indicating that the timer was activated upon startup, your macro security is set too high

5) 要在时间间隔的一次迭代后停用计时器,请添加:如果 TimerID <> 0 然后在子 TriggerTimer 中的 msgbox 语句之后调用 DeactivateTimer

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

别人推荐的

有一点要注意,如果您不检查 TimerID 是否与 TriggerTimer 中的 idevent 相同,您会经常得到,而不是您要求的时间."

"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."

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

这篇关于Outlook VBA - 每半小时运行一次代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆