获取“对象引用未设置为对象的实例”错误,不确定原因

时间:2012-10-16 21:35:41

标签: vb.net add-in office-interop

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

编辑:在单步执行该函数后,看起来第二次围绕循环taskItem即使在设置为“taskrequestItem.GetAssociatedTask(False)”后也等于空,所以在循环的第一次实例中,taskItem是提到即。如果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)

在我添加从每个taskrequestitem获取相关任务项目的部分后,错误开始弹出。

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

2 个答案:

答案 0 :(得分:0)

  

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

Thad仍然会检查

If taskItem.Categories = "TTD"

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

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

答案 1 :(得分:0)

尝试

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

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