vbaoutlook

Outlook events not firing


I'm trying to send an email based on a calendar reminder going off.

I'm having trouble getting VBA macros to recognize that an Outlook event has occurred.

I put this code in a Class Module:

Public WithEvents myOlApp As Outlook.Application

Sub Initialize_handler()
    Set myOlApp = Outlook.Application 'also tried with double quotes around "Outlook.Application"
End Sub

Private Sub myOlApp_Reminder(ByVal Item As Object)
    MsgBox ("test")
End Sub

Private Sub myOlApp_NewMail()
    MsgBox ("test")
End Sub

When I get a new email or a reminder goes off, nothing happens.

I've tested with this macro in a normal module and it works:

Sub MsgBoxTest()
    MsgBox ("test")
End Sub

I have macro settings on "Enable all macros" in the Trust Center.

I've searched google, stackoverflow, a bunch of other websites, and read the documentation on Microsoft.com.

I'm on Outlook 2016 on a PC running Windows 10 Enterprise.


Solution

  • For this method, often used in documentation, run Initialize_handler manually or run it at startup in the special class module ThisOutlookSession.

    Private Sub Application_Startup()
        Initialize_handler
    End Sub