我一直在尝试通过 Outlook 互操作类从 Outlook 中获取约会。特别是那些重复约会的人。我曾尝试使用互操作库的 v12 和 v14 并获得相同的结果。以下代码对我来说总是导致相同的异常。
代码:
Dim pattern As Outlook.RecurrencePattern = appt.GetRecurrencePattern()
Dim recur As Microsoft.Office.Interop.Outlook.AppointmentItem = Nothing
recur = rp.GetOccurrence(Now())
例外:
您更改了此项目的重复周期之一,此实例不再存在。关闭所有打开的项目,然后重试。
注意:我为 GetOccurrence 的参数使用了不同的值,我只使用“now()”来简化代码/问题。所以我认为问题不在于使用 Now()。我尝试了 DateTime.Parse("8/28/2012") 或 DateTime.Parse("8/28/2012 5:00pm") 并抛出了名称异常。
我从这里查看了示例:Question 1,Question 2。两者似乎都没有同样的问题。我已经尝试了关闭对象、释放它们以及将它们归零(无)的每一种排列方式。(例如Microsoft Office 互操作 - 技巧和陷阱)。我直接从 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
谢谢