我一直试图通过Outlook互操作类从Outlook获得约会。特别是那些重复出现的约会。我尝试使用互操作库的v12和v14,结果相同。以下代码对我来说总是会产生相同的异常。
代码:
Dim pattern As Outlook.RecurrencePattern = appt.GetRecurrencePattern()
Dim recur As Microsoft.Office.Interop.Outlook.AppointmentItem = Nothing
recur = rp.GetOccurrence(Now())
例外:
您更改了此项目的一次重复,此实例为no 更长的存在。关闭所有未清项目,然后重试。
注意:我为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)进行了coppied和粘贴示例,结果相同。我完全没有想法!
我在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
由于
答案 0 :(得分:7)
在过去的几个小时里,我一直在努力解决同样的问题,最后我找到了解决方案。似乎GetOccurrence()总是抛出该错误,除非传递的DateTime值与定期约会的实例匹配 - 并且引起我注意的是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#转换的代码的合并,并且尚未通过编译器传递。
答案 1 :(得分:0)
对于VBA for Excel来说,这很愚蠢,但很简单:使用.getOccurrence(x),x必须是Date,而不是Double(并且必须与事件完全匹配)。 将x暗淡为Double obj.getOccurrence(x)不起作用 obj.getOccurrence(CDate(x))正常工作