Office 2010 监控日历事件

Office 2010 monitoring calendar events

嘿,我在 VB.net 中有以下代码,我从 C# 翻译而来,我 found on the internet 用于监视 outlook 日历事件(添加、修改和删除)。

密码是:

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

我目前在网上遇到一个包含以下代码的错误:

_explorer.BeforeFolderSwitch += New ExplorerEvents_10_BeforeFolderSwitchEventHandler(AddressOf Explorer_BeforeFolderSwitch)

错误是:

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.

我需要做什么才能让它在 VB.net 中正常工作?

您需要转换为 VB 的事件接线格式:

AddHandler _explorer.BeforeFolderSwitch, AddressOf Explorer_BeforeFolderSwitch

请注意,显式委托构造是不必要的。