Home > Software engineering >  VBA how to event handle ItemAdd & ItemChange both (for Outlook 2016 Calendar)?
VBA how to event handle ItemAdd & ItemChange both (for Outlook 2016 Calendar)?

Time:09-04

I use the offline version of Microsoft Outlook 2016 Calendar on Windows 10 (64 bit).

GOAL:

When a new appointment is created, OR, when an existing appointment is modified, I would like a Msg box to pop up and display the GlobalAppointmentID of the appointment.

WHAT I HAVE TRIED SO FAR:

Diane Poremsky wrote a good article explaning how to event handle ItemAdd for Mail. I adopted it for Calendar and it worked. This code shown below will display the GlobalAppointmentID as a popup Msg whenever a new appointment is created in Calendar. It works for ItemAdd (without ItemChange):

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
 
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")

'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderCalendar)
Set objItems = objWatchFolder.Items

Set objWatchFolder = Nothing
End Sub



Private Sub objItems_ItemAdd(ByVal Item As Object)

' Your code goes here
' MsgBox "Message subject: " & Item.Subject & vbCrLf & "Message sender: " & Item.SenderName & " (" & Item.SenderEmailAddress & ")"
' https://www.slipstick.com/developer/itemadd-macro

 MsgBox "*** PROPERTIES of olFolderCalendar ***" & vbNewLine & _
        "Subject: " & Item.Subject & vbNewLine & _
        "Start: " & Item.Start & vbNewLine & _
        "End: " & Item.End & vbNewLine & _
        "Duration: " & Item.Duration & vbNewLine & _
        "Location: " & Item.Location & vbNewLine & _
        "Body: " & Item.Body & vbNewLine & _
        "Global Appointment ID: " & Item.GlobalAppointmentID
        
Set Item = Nothing
End Sub

This following code shown below will display a popup Msg of the GlobalAppointmentID, whenever an existing appointment is modified. It works for ItemChange (without ItemAdd):

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
 
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")

'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderCalendar)
Set objItems = objWatchFolder.Items

Set objWatchFolder = Nothing
End Sub

Private Sub objItems_ItemChange(ByVal Item As Object)

 MsgBox "*** PROPERTIES of olFolderCalendar ***" & vbNewLine & _
        "Global Appointment ID: " & Item.GlobalAppointmentID
        
Set Item = Nothing
End Sub

However, when I combined both ItemAdd and ItemChange in the same VBA code, neither of them works. This code shown below does not work for ItemAdd and does not work for ItemChange:

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
 
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")

'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderCalendar)
Set objItems = objWatchFolder.Items

Set objWatchFolder = Nothing
End Sub



Private Sub objItems_ItemAdd(ByVal Item As Object)
    
 MsgBox "*** PROPERTIES of olFolderCalendar ***" & vbNewLine & _
        "Subject: " & Item.Subject & vbNewLine & _
        "Start: " & Item.Start & vbNewLine & _
        "End: " & Item.End & vbNewLine & _
        "Duration: " & Item.Duration & vbNewLine & _
        "Location: " & Item.Location & vbNewLine & _
        "Body: " & Item.Body & vbNewLine & _
        "Global Appointment ID: " & Item.GlobalAppointmentID
        
Set Item = Nothing
End Sub




Private Sub objItems_ItemChange(ByVal Item As Object)

 MsgBox "*** PROPERTIES of olFolderCalendar ***" & vbNewLine & _
        "Global Appointment ID: " & Item.GlobalAppointmentID
        
Set Item = Nothing
End Sub

QUESTION:

How should I correct the code such that both ItemAdd and ItemChange work please? In other words whenever a new appointment is added or an existing appointment is modified, a popup Msg will show the appointment's GlobalAppointmentID.

Thank you.

CodePudding user response:

Problem solved.

The following code captures both ItemAdd and ItemChange if any one is interested.

I made a separate WithEvents and a separate Set ObjItems

Then it worked.

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items
Private WithEvents objItems2 As Outlook.Items

Private Sub Application_Startup()
 
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")

'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderCalendar)
Set objItems = objWatchFolder.Items
Set objItems2 = objWatchFolder.Items

Set objWatchFolder = Nothing
End Sub



Private Sub objItems_ItemAdd(ByVal Item As Object)

' Your code goes here
' MsgBox "Message subject: " & Item.Subject & vbCrLf & "Message sender: " & Item.SenderName & " (" & Item.SenderEmailAddress & ")"
' https://www.slipstick.com/developer/itemadd-macro

 MsgBox "*** PROPERTIES of olFolderCalendar ***" & vbNewLine & _
        "Subject: " & Item.Subject & vbNewLine & _
        "Start: " & Item.Start & vbNewLine & _
        "End: " & Item.End & vbNewLine & _
        "Duration: " & Item.Duration & vbNewLine & _
        "Location: " & Item.Location & vbNewLine & _
        "Body: " & Item.Body & vbNewLine & _
        "Global Appointment ID: " & Item.GlobalAppointmentID
        
Set Item = Nothing
End Sub




Private Sub objItems2_ItemChange(ByVal Item As Object)

 MsgBox "*** PROPERTIES of olFolderCalendar ***" & vbNewLine & _
        "Subject: " & Item.Subject & vbNewLine & _
        "Start: " & Item.Start & vbNewLine & _
        "End: " & Item.End & vbNewLine & _
        "Duration: " & Item.Duration & vbNewLine & _
        "Location: " & Item.Location & vbNewLine & _
        "Body: " & Item.Body & vbNewLine & _
        "Global Appointment ID: " & Item.GlobalAppointmentID
        
Set Item = Nothing
End Sub

CodePudding user response:

Don't set items passed as parameters to Nothing in the event handlers:

Set Item = Nothing

Items that are passed as parameters are released by the caller (Outlook in your case).

There is no need to keep two instances of the Items class in the code to be able to handle events. Try to not release items passed as parameters first.

  • Related