我一直在尝试通过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:00 pm”)并抛出了名称异常。
我从这里查看了样本:Question 1,Question 2。似乎都没有相同的问题。我尝试了关闭对象的所有排列,释放它们,并将它们归零。 (例如Microsoft Office Interop - Tricks and Traps)。我直接从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
谢谢
在过去的几个小时中,我一直在为完全相同的问题而苦苦挣扎,最后我找到了解决方案。除非传递的DateTime值与重复约会的实例匹配,否则似乎GetOccurrence()总是抛出该错误-令我失望的是,DateTime值必须在两个Date AND时间上都匹配。
这是您的代码版本,它从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
请注意,我是C#开发人员,因此上面可能有语法错误,因为这是OP的代码和我的代码从C#转换而来的合并,并且没有通过编译器传递。
对于VBA for Excel,这很愚蠢,但很简单:使用.getOccurrence(x),x必须是Date,而不是Double(并且必须与事件完全匹配)。将x暗淡为Doubleobj.getOccurrence(x)不起作用obj.getOccurrence(CDate(x))正常工作