Help on Scheduling a macro RRS feed

  • Question

  • I have a macro written in MS OUTLOOK. I would like to execute it based on some condition and/or time interval. I looked at the rule and it has something like "Run A script" as an action for the rule. But When I try to select a script, it does not show the macro in the script dialog. Any thoughts?

    Also please advise how do we schedule it so that it runs at regular intervals (say Daily).

    thanks for your help

    Suresh Murugesan

    Thursday, May 21, 2015 12:15 AM


  • Hello Suresh,

    The macro sub should look like the following one to see it in the Rules window for choosing VBA macros:

    Public Sub YourMacroName(mail as MailItem)
      ' do whatever you need
    End Sub

    But to run the code periodically you need to use a timer. 

    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 lngTimerID As Long
    Public Sub ActivateTimer(ByVal lngMinutes As Long)
        'The SetTimer call accepts milliseconds, so convert to minutes'
        lngMinutes = lngMinutes * 1000 * 60
        'Check to see if timer is running before call to SetTimer
        If lngTimerID <> 0 Then Call DeactivateTimer
        lngTimerID = SetTimer(0, 0, lngMinutes, AddressOf TriggerTimer)
        If lngTimerID = 0 Then
            MsgBox "The timer failed to activate."
        End If
    End Sub
    Public Sub DeactivateTimer()
        Dim lSuccess As Long
        lSuccess = KillTimer(0, lngTimerID)
        If lSuccess = 0 Then
            MsgBox "The timer failed to deactivate."
            lngTimerID = 0
        End If
    End Sub
    Private Sub TriggerTimer(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
        'Change the name of the macro to run on the next line'
    End Sub
    Sub RunTimer()
        ActivateTimer 10
    End Sub

    • Marked as answer by L.HlModerator Tuesday, June 2, 2015 11:58 PM
    Thursday, May 21, 2015 6:47 AM