下面是完整的功能,但这是我认为导致错误的代码区域:
编辑:在单步执行该函数后,即使在设置为“taskrequestItem.GetAssociatedTask(False)”之后,循环任务项的第二次似乎也等于没有,因此在该循环的实例中,第一次引用任务项即. 如果 taskitem.categories = "TTD" 则抛出异常。但是,我在if语句中添加了“AND taskItem IsNot Nothing”的条件,我仍然得到这个异常。
If (TypeOf (resultItem) Is Outlook._TaskRequestItem) Then
counter += 1
taskrequestItem = resultItem
taskItem = taskrequestItem.GetAssociatedTask(False)
'taskItem.PercentComplete = 10
'If Not (taskItem.Status = Outlook.OlTaskStatus.olTaskComplete) Then
'strBuilder.AppendLine("#" + counter.ToString() + _
'" 2duedate: " + )
If taskItem.Categories = "TTD" Then
TTDcounter += 1
Dim listarray() As String = {taskItem.Delegator, taskItem.Subject, taskItem.DueDate, stripEstComp(taskItem.Body.ToString())}
taskPaneControl3.ListView2.Items.Add("New").SubItems.AddRange(listarray)
End If
End If
'End If
Marshal.ReleaseComObject(resultItem)
resultItem = folderItems.FindNext()
Loop Until IsNothing(resultItem)
在我添加了从每个任务请求项中获取关联任务项的部分后,错误开始弹出。
Private Sub GetTaskRequests(objApp As Microsoft.Office.Interop.Outlook.Application)
Dim folder As Outlook.Folder = _
TryCast(Application.Session.Folders(email).Folders("Inbox"), Outlook.Folder)
Dim searchCriteria As String = "[MessageClass] = 'IPM.TaskRequest'"
Dim strBuilder As StringBuilder = Nothing
Dim counter As Integer = 0
Dim taskrequestItem As Outlook.TaskRequestItem = Nothing
Dim taskItem As Outlook.TaskItem = Nothing
Dim folderItems As Outlook.Items = Nothing
Dim resultItem As Object = Nothing
Dim TTDcounter As Integer = 0
Try
folderItems = folder.Items
folderItems.IncludeRecurrences = True
If (folderItems.Count > 0) Then
resultItem = folderItems.Find(searchCriteria)
If Not IsNothing(resultItem) Then
strBuilder = New StringBuilder()
Do
If (TypeOf (resultItem) Is Outlook._TaskRequestItem) Then
counter += 1
taskrequestItem = resultItem
taskItem = taskrequestItem.GetAssociatedTask(False)
'taskItem.PercentComplete = 10
'If Not (taskItem.Status = Outlook.OlTaskStatus.olTaskComplete) Then
'strBuilder.AppendLine("#" + counter.ToString() + _
'" 2duedate: " + )
'If taskItem.Categories = "TTD" Then
TTDcounter += 1
Dim listarray() As String = {taskItem.Delegator, taskItem.Subject, taskItem.DueDate, stripEstComp(taskItem.Body.ToString())}
taskPaneControl3.ListView2.Items.Add("New").SubItems.AddRange(listarray)
'End If
End If
'End If
Marshal.ReleaseComObject(resultItem)
resultItem = folderItems.FindNext()
Loop Until IsNothing(resultItem)
End If
End If
If Not IsNothing(strBuilder) Then
Debug.WriteLine(strBuilder.ToString())
Else
Debug.WriteLine("There is no match in the " + _
folder.Name + " folder.")
End If
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
Debug.WriteLine("gettaskrequest")
Finally
If Not IsNothing(folderItems) Then Marshal.ReleaseComObject(folderItems)
End Try
End Sub