item ejemplos application app vba outlook-vba

ejemplos - Outlook VBA-Ejecutar un código cada media hora



vba outlook item properties (3)

Corregir para la respuesta superior para 64 bits:

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

Quiero ejecutar un código específico en Outlook (VBA) cada media hora.

Además, el usuario de Outlook no debe molestarse cuando se ejecuta el código. Debería ejecutarse en back-end solamente.

Hay un evento llamado Application_Reminder . Se ejecuta cuando a cada vez que aparece un recordatorio en Outlook. Pero esto todavía implica la interacción del usuario. Quiero un procedimiento completo de back-end.


Para Win64, necesitaba cambiarlo a esto:

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


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

Coloque el siguiente código en el módulo ThisOutlookSession (Herramientas-> Macros-> Editor 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

Coloque el siguiente código en un nuevo módulo 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

Puntos clave:

1) Esta función de temporizador no requiere que una ventana particular esté abierta; funciona en el fondo

2) Si no desactiva el temporizador cuando se cierra la aplicación, es probable que se bloquee

3) El ejemplo muestra cómo se activa el temporizador en el inicio, pero puede ser llamado fácilmente por un evento diferente

4) Si no ve el msgbox indicando que el temporizador se activó en el inicio, la seguridad de su macro es demasiado alta

5) Para que el temporizador se desactive después de una iteración del intervalo de tiempo, agregue: Si TimerID <> 0 Luego llame a DeactivateTimer después de la instrucción msgbox en el sub TriggerTimer

Alguien más sugirió

"un punto a tener en cuenta, si no verifica si TimerID es lo mismo que idevent en el TriggerTimer, obtendrá cada cierto tiempo, y no la hora que solicitó".

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