Search code examples
vbaoutlookprocedure

How to temporarily stop event script on incoming emails?


I process incoming emails with

Private WithEvents objNewMailItems As Outlook.Items

as startup procedure and

Private Sub objNewMailItems_ItemAdd(ByVal item As Object)

executing whenever a new email arrives in the inbox.

How can I (manually) stop this procedure from executing?


Solution

  • objNewMailItems is declared as an event provider; as long as its reference is set, it will fire events.

    To make it stop, you need to set its reference to Nothing.

    Public Sub StopHandlingNewMailItems()
        Set objNewMailItems = Nothing
    End Sub
    

    Take the code you have that's currently assigning that object reference, move it to some StartHandlingNewMailItems() procedure, and invoke it at startup; now you can toggle handling new mail items on/off at will.