none
Initialize Handler RRS feed

  • Question

  • Hi All,

    I would really appreciate your help if you could explain how to create new events in Outlook VBA.

    I read the documentation but I still have questions.

    First of all, it seems that I have to register the event handler in one of the Application evenest (i.e. run Initialize_handler in Application_ItemLoad). The whole point is that the Application events are not sufficient. I need to define a new event Item_change that works independently, but I cannot initialize my item.

    Public WithEvents myMailItem as Outlook.Mailitem public sub Initialize_Handler() set myMailItem = Outlook.Application.ActiveExplorer.Selection.Item(1) end sub

    Private Sub myMailItem_ItemChange(Item as Object, Cancel as Boolean) <<<logic>>> End Sub

    Again, I would really appreciate if anyone could explain the logic.

    Thank you !


    Monday, July 10, 2017 5:12 PM

Answers

  • I need to define a new event Item_change that works independently

    No chance, you can only use the predefined events.

    The line
      Public WithEvents myMailItem As Outlook.MailItem
    just declares an object variable and the keyword WithEvents enables the Event-Routines for that object.

    Any Event-Routine that you write at design time is not associated to a special object, later at run time you have to initialize the object variable at an appropriate point to "activate" your Event-Routines.

    Andreas.

    Sunday, July 16, 2017 5:39 AM

All replies

  • I need to define a new event Item_change that works independently

    No chance, you can only use the predefined events.

    The line
      Public WithEvents myMailItem As Outlook.MailItem
    just declares an object variable and the keyword WithEvents enables the Event-Routines for that object.

    Any Event-Routine that you write at design time is not associated to a special object, later at run time you have to initialize the object variable at an appropriate point to "activate" your Event-Routines.

    Andreas.

    Sunday, July 16, 2017 5:39 AM
  • Thank you Andreas. 

    My goal was to use Flag as Completed as a trigger for several actions: 

    • move to a different folder
    • auto-populate custom fields
    • etc. 

    In the end I used:

    1. Application_ItemLoad
    2. xItem_ItemChange
    Private Sub Application_ItemLoad(ByVal item As Object)
            
            Dim xFolder As Folder: Set xFolder = Nothing
            Set xItems = Nothing
        
            Set xFolder = Application.ActiveExplorer.CurrentFolder
            Set xItems = xFolder.items
        
    End Sub
    Private Sub xItems_ItemChange(ByVal item As Object)
    'this section contains the values after they have been changed'
        
        If TypeName(item) <> "MailItem" Then
            End
        End If
    
        If item.FlagStatus = olFlagComplete Then
            With item
                .UnRead = False
                .Save
            End With
        End If
     
     End Sub

    Thursday, May 2, 2019 1:08 PM