在共享日历中创建AppointmentItems-找不到对象

时间:2019-04-25 22:26:16

标签: vb.net outlook office-interop

我正在尝试创建一个应用程序,该应用程序允许用户使用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时会引发异常-说“尝试的操作失败。找不到对象。当日历所有者登录时它可以工作的事实使我相信我不了解如何从其他帐户获取文件夹。我靠近吗?我知道接收者对象,并使用Outlook.Namespace.CreateRecipientNameSpace.GetShareDefaultFolder创建并稍后对其进行解析,但是我尝试过的所有对象组合都以完全相同的方式失败了。我觉得我想念一些愚蠢的东西。

1 个答案:

答案 0 :(得分:0)

能够使这一功能正常运行(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

编辑:我认为应该说,如果日历/文件夹停留在原处且不会移动,则该解决方案仅对我有用。如果我理解正确,则如果日历已重定位,EntryID也将更改。我不确定还有什么会触发ID的更改(可能正在重命名?等),但是我不明白为什么我不能仅仅更新ID以反映将来的更改。这对我有用。