0

下面是完整的功能,但这是我认为导致错误的代码区域:

编辑:在单步执行该函数后,即使在设置为“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
4

2 回答 2

0

但是,我在if语句中添加了“AND taskItem IsNot Nothing”的条件,我仍然得到这个异常。

泰德仍然会检查

If taskItem.Categories = "TTD"

第一的。测试taskItem isNot Nothing需要首先完成,你需要一个短路逻辑And,那就是

If taskItem isNot Nothing AndAlso taskItem.Categories = "TTD" Then
于 2012-10-16T22:14:16.117 回答
0

尝试

If taskItem IsNot Nothing AndAlso taskItem.Categories = "TTD" Then

如果没有AndAlso运算符,条件评估将继续检查 taskItem.Categories 并且您的代码将崩溃。当然,在这种情况下,评估的顺序有很大的不同。

于 2012-10-16T22:16:25.260 回答