gpt4 book ai didi

vba - Outlook VBA - 每半小时运行一次代码

转载 作者:行者123 更新时间:2023-12-02 05:34:07 26 4
gpt4 key购买 nike

我想每半小时在 Outlook(VBA) 中运行一次特定代码。

此外,代码运行时 Outlook 用户不应受到干扰。它应该仅在后端运行。

有一个名为 Application_Reminder 的事件。当 Outlook 中每次出现提醒时,它就会运行。但这仍然涉及用户交互。我想要一个完整的后端程序。

最佳答案

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 在子 TriggerTimer 中的 msgbox 语句之后调用 DeactivateTimer

有人建议

“需要注意的一点是,如果你不检查 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

关于vba - Outlook VBA - 每半小时运行一次代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com