StealthRT StealthRT - 7 months ago 26
Vb.net Question

Office 2010 monitoring calendar events

Hey all I have the following code in VB.net that i translated from C# that I found on the internet for monitoring a outlook calendar events (Add, modify and delete).

The code is:

Public Class ThisAddIn
Private Sub ThisAddIn(sender As Object, e As System.EventArgs) Handles Me.Startup
Dim monitor As New CalendarMonitor(Me.Application.ActiveExplorer())

AddHandler monitor.AppointmentAdded, AddressOf monitor_AppointmentAdded
AddHandler monitor.AppointmentModified, AddressOf monitor_AppointmentModified
AddHandler monitor.AppointmentDeleting, AddressOf monitor_AppointmentDeleting
End Sub
...etc etc...
End Class


=======================

Public Class CalendarMonitor
Private _explorer As Explorer
Private _folderPaths As List(Of String)
Private _calendarFolders As List(Of MAPIFolder)
Private _calendarItems As List(Of Items)
Private _deletedItemsFolder As MAPIFolder

Public Event AppointmentAdded As EventHandler(Of EventArgs(Of AppointmentItem))
Public Event AppointmentModified As EventHandler(Of EventArgs(Of AppointmentItem))
Public Event AppointmentDeleting As EventHandler(Of CancelEventArgs(Of AppointmentItem))

Public Sub New(anExplorer As Explorer)
_folderPaths = New List(Of String)()
_calendarFolders = New List(Of MAPIFolder)()
_calendarItems = New List(Of Items)()

_explorer = anExplorer
_explorer.BeforeFolderSwitch += New ExplorerEvents_10_BeforeFolderSwitchEventHandler(AddressOf Explorer_BeforeFolderSwitch)

Dim session As [NameSpace] = _explorer.Session
Try
_deletedItemsFolder = session.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems)
HookupDefaultCalendarEvents(session)
Finally
Marshal.ReleaseComObject(session)
session = Nothing
End Try
End Sub

Private Sub HookupDefaultCalendarEvents(aSession As [NameSpace])
Dim folder As MAPIFolder = aSession.GetDefaultFolder(OlDefaultFolders.olFolderCalendar)
If folder IsNot Nothing Then
Try
HookupCalendarEvents(folder)
Finally
Marshal.ReleaseComObject(folder)
folder = Nothing
End Try
End If
End Sub

Private Sub Explorer_BeforeFolderSwitch(aNewFolder As Object, ByRef Cancel As Boolean)
Dim folder As MAPIFolder = TryCast(aNewFolder, MAPIFolder)
'
' Hookup events to any other Calendar folder opened.
'
If folder IsNot Nothing Then
Try
If folder.DefaultItemType = OlItemType.olAppointmentItem Then
HookupCalendarEvents(folder)
End If
Finally
Marshal.ReleaseComObject(folder)
folder = Nothing
End Try
End If
End Sub

Private Sub HookupCalendarEvents(aCalendarFolder As MAPIFolder)
If aCalendarFolder.DefaultItemType <> OlItemType.olAppointmentItem Then
Throw New ArgumentException("The MAPIFolder must use " + "AppointmentItems as the default type.")
End If

If (_folderPaths.Contains(aCalendarFolder.FolderPath) = False) AndAlso (IsUsersCalendar(aCalendarFolder)) Then
Dim items As Items = aCalendarFolder.Items

_folderPaths.Add(aCalendarFolder.FolderPath)
_calendarFolders.Add(aCalendarFolder)
_calendarItems.Add(items)

items.BeforeItemMove += New MAPIFolderEvents_12_BeforeItemMoveEventHandler(AddressOf Calendar_BeforeItemMove)
items.ItemChange += New ItemsEvents_ItemChangeEventHandler(AddressOf CalendarItems_ItemChange)
items.ItemAdd += New ItemsEvents_ItemAddEventHandler(AddressOf CalendarItems_ItemAdd)
End If
End Sub

Private Sub CalendarItems_ItemAdd(anItem As Object)
Dim appointment As AppointmentItem = TryCast(anItem, AppointmentItem)
If appointment IsNot Nothing Then
Try
RaiseEvent AppointmentAdded(Me, New EventArgs(Of AppointmentItem)(appointment))
Finally
Marshal.ReleaseComObject(appointment)
appointment = Nothing
End Try
End If
End Sub

Private Sub CalendarItems_ItemChange(anItem As Object)
Dim appointment As AppointmentItem = TryCast(anItem, AppointmentItem)
If appointment IsNot Nothing Then
Try
RaiseEvent AppointmentModified(Me, New EventArgs(Of AppointmentItem)(appointment))
Finally
Marshal.ReleaseComObject(appointment)
appointment = Nothing
End Try
End If
End Sub

Private Sub Calendar_BeforeItemMove(anItem As Object, aMoveToFolder As MAPIFolder, ByRef Cancel As Boolean)
If (aMoveToFolder Is Nothing) OrElse (IsDeletedItemsFolder(aMoveToFolder)) Then
Dim appointment As AppointmentItem = TryCast(anItem, AppointmentItem)

If appointment IsNot Nothing Then
Try
'If AppointmentDeleting IsNot Nothing Then
'
' Listeners to the AppointmentDeleting event can cancel the move operation if moving
' to the deleted items folder.
'
Dim args As New CancelEventArgs(Of AppointmentItem)(appointment)
RaiseEvent AppointmentDeleting(Me, args)

Cancel = args.Cancel
'End If
Finally
Marshal.ReleaseComObject(appointment)
appointment = Nothing
End Try
End If
End If
End Sub

Private Function IsUsersCalendar(aFolder As MAPIFolder) As Boolean
'
' This is based purely on my observations so far - a better way?
'
Return (aFolder.Store IsNot Nothing)
End Function

Private Function IsDeletedItemsFolder(aFolder As MAPIFolder) As Boolean
Return (aFolder.EntryID = _deletedItemsFolder.EntryID)
End Function
End Class


And I am currently hitting an error on line that has this code:

_explorer.BeforeFolderSwitch += New ExplorerEvents_10_BeforeFolderSwitchEventHandler(AddressOf Explorer_BeforeFolderSwitch)


The error is:


An exception of type 'System.MissingMemberException' occurred in
Microsoft.VisualBasic.dll but was not handled in user code

Additional information: Public member 'BeforeFolderSwitch' on type
'ExplorerClass' not found.


What do I need to do in order to get it working in VB.net?

Answer

You need to convert to VB's event wireup format:

AddHandler _explorer.BeforeFolderSwitch, AddressOf Explorer_BeforeFolderSwitch

Note that the explicit delegate construction is unnecessary.

Comments