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

问题描述 投票:0回答:1

我正在尝试创建一个应用程序,允许用户使用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("[email protected]") '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.CreateRecipient以及NameSpace.GetShareDefaultFolder解析它,但我尝试过的每个组合都以完全相同的方式失败。我觉得我错过了一些愚蠢的东西。

vb.net outlook office-interop
1个回答
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来反映未来的变化。这适合我。

© www.soinside.com 2019 - 2024. All rights reserved.