GetOccurrence总是抛出异常

Sen*_*Sen 6 vb.net outlook office-interop

我一直试图通过Outlook互操作类从outlook中获得约会.特别是那些重复出现的约会.我尝试使用互操作库的v12和v14,结果相同.以下代码对我来说总是会产生相同的异常.

码:

Dim pattern As Outlook.RecurrencePattern = appt.GetRecurrencePattern()
Dim recur As Microsoft.Office.Interop.Outlook.AppointmentItem = Nothing
recur = rp.GetOccurrence(Now())
Run Code Online (Sandbox Code Playgroud)

例外:

您更改了此项目的其中一个重复项,并且此实例不再存在.关闭所有未清项目,然后重试.

注意:我对GetOccurrence的参数使用了不同的值,我只使用"now()"来简化代码/问题.所以我不相信问题在于使用Now().我尝试了DateTime.Parse("8/28/2012")或DateTime.Parse("8/28/2012 5:00 pm"),并抛出了名称异常.

我从这里看了样品:问题1,问题2.似乎都没有同样的问题.我已经尝试了关闭对象的每个排列,释放它们,并将它们归零(无).(例如Microsoft Office Interop - 技巧和陷阱).我直接从MSDN(例如:MDSN)中删除并粘贴了相同结果的示例.我完全没有想法!

我在Windows Server 2008 R2 64位操作系统上运行,使用Visual Studio 2010,.NET 4.0和Outlook 2007.

这是一个更完整的代码示例,它总是为我抛出异常:

    Public Sub TestOutlook()
    Dim oApp As Outlook.Application = Nothing
    Dim mapiNamespace As Outlook.[NameSpace] = Nothing
    Dim calFolder As Outlook.MAPIFolder = Nothing
    Dim calItems As Outlook.Items = Nothing

    oApp = New Outlook.Application()
    mapiNamespace = oApp.GetNamespace("MAPI")
    calFolder = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
    calItems = calFolder.Items
    calItems.IncludeRecurrences = True

    For itemIndex As Integer = 1 To calItems.Count

        Dim item As Outlook.AppointmentItem = Nothing
        item = calFolder.Items.Item(itemIndex)

        If item.IsRecurring Then
            Dim rp As Outlook.RecurrencePattern = Nothing
            rp = item.GetRecurrencePattern()
            item.Close(Outlook.OlInspectorClose.olDiscard)
            CleanUpComObject(item)
            item = Nothing
            GC.Collect()

            Try
                rp.GetOccurrence(Now)
            Catch ex As System.Exception
                Debug.WriteLine("Ex with GetOccurrence: " & ex.Message)
            End Try

        End If
        If item IsNot Nothing Then item.Close(Outlook.OlInspectorClose.olDiscard)
        CleanUpComObject(item)
        item = Nothing
        GC.Collect()
    Next

    CleanUpComObject(calItems)
    CleanUpComObject(calFolder)
    CleanUpComObject(mapiNamespace)
    oApp.Quit()
    CleanUpComObject(oApp)
    GC.Collect()
End Sub

Private Sub CleanUpComObject(obj As Object)
    Try
        If obj IsNot Nothing AndAlso System.Runtime.InteropServices.Marshal.IsComObject(obj) Then
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
            obj = Nothing
        End If
    Catch ex As System.Exception
        Debug.WriteLine("Exception in Clean up: " & ex.Message)
    End Try
End Sub
Run Code Online (Sandbox Code Playgroud)

谢谢

pau*_*ulH 7

在过去的几个小时里,我一直在努力解决同样的问题,最后我找到了解决方案.似乎GetOccurrence()总是抛出该错误,除非传递的DateTime值与定期约会的实例匹配 - 并且引起我注意的是DateTime值必须在日期时间上匹配.

这是您的代码版本,它从RecurrencePattern获取时间并将其添加到传递给GetOccurence()的DateTime值,然后应该正确地找到所选日期的任何约会实例.

Dim item As Outlook.AppointmentItem = Nothing 
item = calFolder.Items.Item(itemIndex) 

If item.IsRecurring Then 
    Dim rp As Outlook.RecurrencePattern = Nothing 
    rp = item.GetRecurrencePattern() 

    Dim dt2 as DateTime = DateTime.Parse("8/28/2012")
    dt2 = dt2.AddHours(item.Start.Hour)
    dt2 = dt2.AddMinutes(item.Start.Minute)
    dt2 = dt2.AddSeconds(item.Start.Second)

    item.Close(Outlook.OlInspectorClose.olDiscard) 
    CleanUpComObject(item) 
    item = Nothing 
    GC.Collect()  

    Try 
        rp.GetOccurrence(dt2)

    Catch ex1 As System.Runtime.InteropServices.COMException

// Do Nothing, let this error go.  No instance of the appointment falls on this date.

    Catch ex As System.Exception
        Debug.WriteLine("Ex with GetOccurrence: " & ex.Message) 
    End Try 

End If 
Run Code Online (Sandbox Code Playgroud)

请注意,我是C#开发人员,因此上面可能存在语法错误,因为它是OP代码和从C#转换的代码的合并,并且尚未通过编译器传递.