在共享日历中创建 AppointmentItems - 未找到对象异常

Creating AppointmentItems in shared calendars - object not found exception

我正在尝试创建一个应用程序,允许用户使用 Outlook Interop 在共享的 Outlook 日历上为我们的企业创建约会项目(你是这么说的吗?)。

日历位于我的帐户中,我已授予需要它的每个人的权限。这些用户能够从他们的真实 Outlook 客户端毫无问题地创建和修改日历。我已经编写了以下函数,当它 运行 我的帐户已登录时,它可以完美地工作。 当我注销并进入其他用户帐户之一时,它会抛出一个例外。

Public Sub AddAppointment()
    Try
        Dim Application As Outlook.Application = New Outlook.Application
        Dim NS As Outlook.NameSpace = Application.GetNamespace("MAPI")
        Dim RootFolder As Outlook.Folder
        Dim CalendarFolder As Outlook.Folder
        Dim PlumbingCalendarFolder As Outlook.Folder
        Dim Appointment As Outlook.AppointmentItem

        RootFolder = NS.Folders("myemail@ourdomain.com") 'exception here
        CalendarFolder = RootFolder.Folders("Calendar")
        FletcherCalendarFolder = CalendarFolder.Folders("Plumbing Tasks")
        Appointment = FletcherCalendarFolder.Items.Add("IPM.Appointment")

        'with/end with guts that define the appointmentitem here

        Appointment.Save()

        MessageBox.Show("An event for this due date was added to the calendar.")

        Application = Nothing
    Catch ex As Exception
        MessageBox.Show("The event for this due date could not be added to the calendar. The following error occurred: " & ex.Message)
    End Try
End Sub

当我尝试设置 RootFolder 时抛出异常 - 说 'The attempted operation failed. An object could not be found.' 它在日历所有者登录时起作用的事实让我相信我不明白我应该如何获取文件夹来自不同的帐户。我很接近吗?我知道接收者对象并使用 Outlook.Namespace.CreateRecipientNameSpace.GetShareDefaultFolder 创建并稍后解析它,但是我尝试过的每个组合都以完全相同的方式失败。我觉得我错过了一些愚蠢的东西。

能够使这个工作正常 (FeelsGoodMan)。当我发现 Outlook.NameSpace.GetFolderFromID 时,我放弃了以以前的方式获取我的日历文件夹的想法。 EntryID 显然是 Outlook 对象的唯一标识符(?不要引用我的话。)通过监视哪些文件夹起作用,我能够获得日历的 EntryID,然后通过使用 GetFolderFromID 我能够在我的代码中获取一个工作文件夹。

Public Sub AddAppointment()
    Const ENTRYID As String = "IdIGotFromWatch"
    Try
        Dim Application As Outlook.Application = New Outlook.Application
        Dim NS As Outlook.NameSpace = Application.GetNamespace("MAPI")
        Dim CalendarFolder As Outlook.Folder
        Dim Appointment As Outlook.AppointmentItem

        CalendarFolder = NS.GetFolderFromID(ENTRYID)

        Appointment = CalendarFolder.Items.Add("IPM.Appointment")

        'with/end with guts that define the appointmentitem here

        Appointment.Save()

        Application = Nothing
    Catch ex As Exception
        MessageBox.Show("The event for this due date could not be added to the calendar. The following error occurred: " & ex.Message)
    End Try
End Sub

编辑:我认为应该说这个解决方案只有在 Calendar/Folder 保持原位不动的情况下才对我有用。如果我理解正确,如果日历被重新定位,EntryID 也会改变。我不确定还有什么会触发 ID 的更改(也许重命名?等等),但我不明白为什么我不能只更新 ID 以反映将来的更改。这对我有用。