none
Random ocuurence of error when Setting com object to nothing RRS feed

  • Question

  • Hi,

    i have a code in my addin, something like

    Dim SearchItems As Outlook.Items = Nothing

    Dim CalendarFolderItems As Outlook.Items = Nothing

    Dim index As Integer

    '' at this collection count > 0 and  CalendarCollection(index).EmailAddress = email comes to be true for atlest once

    For index = 1 To CalendarCollection.Count
    If (CalendarCollection(index).EmailAddress = email) Then
    CalendarFolderItems = CalendarCollection(index).FolderItems
    SearchItems = CalendarFolderItems.Restrict("My search query")
    If (SearchItems.Count > 0) Then
    SearchItems = Nothing
    Exit For
    Else
    SearchItems = Nothing
    End If
    End If
    Next

    ' some code fetching data from database

    Try

    If SearchItems Is Nothing Then SearchItems = CalendarFolderItems.Restrict("another sercah query")

    If SearchItems.Count > 0 Then

    ' some code

                   end if

           end if

    catch

    Finally

    NAR(SearchItems)

    end Sub

    Public Sub NAR(ByRef o As Object)
    Dim Count As Integer = 1
    Try
    Count = System.Runtime.InteropServices.Marshal.ReleaseComObject(o)

    'End While
    Catch
    Finally
    o = Nothing
    End Try
    End Sub

    Now, sometimes while debugging i found when i try to check SearchItems.count, it diplays "unable to evaluate expression" and after that it just come out from the whole function. 

    Is it due to i am setting SerachItems to nothing and by the time addin search for the second search, SerachItems has got null reference? 

    i am not getting why it happens sometime not everytime,  i am relying my sarch query will return atleaset one result

    and i initialise some variables in

    If SearchItems.Count > 0 Then

    ' some code

    end if

    Please help, if i am doing something wrong

     

    Thanks

    Thursday, April 17, 2014 3:17 PM

Answers

  • For example, the How To: Use Restrict method to retrieve Outlook mail items from a folder article provides the following sample code in VB.NET, I tested it in VSTO based add-ins:

    Imports System.Runtime.InteropServices
    Imports System.Diagnostics
    ' ...
    Private Sub RestrictUnreadItems(folder As Outlook.MAPIFolder)
        Dim restrictCriteria As String = "[UnRead] = true"
        Dim strBuilder As StringBuilder = Nothing
        Dim folderItems As Outlook.Items = Nothing
        Dim resultItems As Outlook.Items = Nothing
        Dim mail As Outlook._MailItem = Nothing
        Dim counter As Integer = 0
        Dim item As Object = Nothing
        Try
            strBuilder = New StringBuilder()
            folderItems = folder.Items
            resultItems = folderItems.Restrict(restrictCriteria)
            item = resultItems.GetFirst()
            While Not IsNothing(item)
                If TypeOf (item) Is Outlook._MailItem Then
                    counter += 1
                    mail = item
                    strBuilder.AppendLine("#" + counter.ToString() + _
                                         " - Subject: " + mail.Subject)
                End If
                Marshal.ReleaseComObject(item)
                item = resultItems.GetNext()
            End While
            If (strBuilder.Length > 0) 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)
        Finally
            If Not IsNothing(folderItems) Then Marshal.ReleaseComObject(folderItems)
            If Not IsNothing(resultItems) Then Marshal.ReleaseComObject(resultItems)
        End Try
    End Sub


    As you may see in the code the GetFirst method is used instead of checking the Count property.
    Thursday, April 17, 2014 3:33 PM

All replies

  • Hello,

    I'd recommend to release all underlying COM objects instantly, not in the while loop. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    Thursday, April 17, 2014 3:29 PM
  • For example, the How To: Use Restrict method to retrieve Outlook mail items from a folder article provides the following sample code in VB.NET, I tested it in VSTO based add-ins:

    Imports System.Runtime.InteropServices
    Imports System.Diagnostics
    ' ...
    Private Sub RestrictUnreadItems(folder As Outlook.MAPIFolder)
        Dim restrictCriteria As String = "[UnRead] = true"
        Dim strBuilder As StringBuilder = Nothing
        Dim folderItems As Outlook.Items = Nothing
        Dim resultItems As Outlook.Items = Nothing
        Dim mail As Outlook._MailItem = Nothing
        Dim counter As Integer = 0
        Dim item As Object = Nothing
        Try
            strBuilder = New StringBuilder()
            folderItems = folder.Items
            resultItems = folderItems.Restrict(restrictCriteria)
            item = resultItems.GetFirst()
            While Not IsNothing(item)
                If TypeOf (item) Is Outlook._MailItem Then
                    counter += 1
                    mail = item
                    strBuilder.AppendLine("#" + counter.ToString() + _
                                         " - Subject: " + mail.Subject)
                End If
                Marshal.ReleaseComObject(item)
                item = resultItems.GetNext()
            End While
            If (strBuilder.Length > 0) 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)
        Finally
            If Not IsNothing(folderItems) Then Marshal.ReleaseComObject(folderItems)
            If Not IsNothing(resultItems) Then Marshal.ReleaseComObject(resultItems)
        End Try
    End Sub


    As you may see in the code the GetFirst method is used instead of checking the Count property.
    Thursday, April 17, 2014 3:33 PM
  • sorry for the late response

    Thanks for your Reply Eugene, i will look into this

    Tuesday, May 20, 2014 10:19 AM