none
VSTO and Marshal.ReleaseComObject RRS feed

  • Question

  • This is my code:

      Public Shared Sub AddCustomPropertyToMAPIFolder(ByVal fldFolder As Outlook.MAPIFolder, ByVal strFieldName As String, ByVal lngType As Outlook.OlUserPropertyType)
        Dim objItem As Object = Nothing
        Dim objUserPropId As Outlook.UserProperty = Nothing
    
        Try
          ' Fictive item
          ' We can only add folder fields to a particular item
          objItem = fldFolder.Items.Add(fldFolder.DefaultMessageClass)
          'objUserPropId = fldFolder.Items.Add(fldFolder.DefaultMessageClass).UserProperties.Add(strFieldName, lngType, True)
          objUserPropId = objItem.UserProperties(strFieldName)
          If Not objUserPropId Is Nothing Then
            objUserPropId.Delete()
          End If
          If Not fldFolder.UserDefinedProperties(strFieldName) Is Nothing Then
            fldFolder.UserDefinedProperties(strFieldName).Delete()
          End If
          objUserPropId = objItem.UserProperties.Add(strFieldName, lngType, True)
    
          ' To prevent reminder event in case of Calendars
          ' set Start date as far as it possible
          If fldFolder.DefaultItemType = OlItemType.olAppointmentItem Then
            Dim a As Outlook.AppointmentItem = CType(objItem, Outlook.AppointmentItem)
            a.Start = Date.MaxValue
          End If
    
          objItem.Save()
          ' delete the fictive item
          objItem.Delete()
    
        Finally
          If Not objItem Is Nothing Then
            Marshal.ReleaseComObject(objItem)
          End If
        End Try
      End Sub
    

    Given the code above, is Marshal.ReleaseComObject on the right place in finally clause in this code? Or there's no need in it at  all?

    Friday, March 25, 2011 10:37 PM

Answers