Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.O

  • Thread starter Thread starter Dhananjay
  • Start date Start date
D

Dhananjay

Hi All,
I am developing one VSTO COM addin for outlook 2003 in vb 2005. My
code snippet is as follows. odbcrdr(0) always contains valid entryID.
'-------------------------------------------------------------------------------------------------------------------------------------------------------
' Set connection & command -----
' ----
Dim odbcrdr As OdbcDataReader
odbcrdr = myCmd.ExecuteReader
Dim cnt As Integer = 0
Dim c As Outlook.ContactItem
Dim ns As Outlook.NameSpace
ns = myGlobalApplication.GetNamespace("MAPI")
While odbcrdr.Read
Try
c = Nothing
c = ns.GetItemFromID(odbcrdr(0))
Catch ex As System.Exception
AddTrace("Contact item can not be set for
entryid - " & odbcrdr(0) & vbCrLf & "Exception - " & ex.Message)
MsgBox("Contact item can not be set for entryid
- " & odbcrdr(0) & vbCrLf & "Exception - " & ex.Message,
MsgBoxStyle.Critical Or MsgBoxStyle.SystemModal)
Finally
c = Nothing
End Try
End While
'-------------------------------------------------------------------------------------------------------------------------------------------------------

Now when the count of returned records is greater than 250, I am
always getting error on record 251 -
Exception - Unable to cast COM object of type 'System.__ComObject' to
interface type 'Microsoft.Office.Interop.Outlook.ContactItem'. This
operation failed because the QueryInterface call on the COM component
for the interface with IID '{00063021-0000-0000-C000-000000000046}'
failed due to the following error: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
This error is always appearing on the record 251! One thing is that,
all these records are on my public folder. (If those entryids are from
personal folder then code is working fine!!)
Please Help!

Thanks,
Dhananjay
 
You are running out of the 255 open messages limit impsed by Exchange.
Release the previously opened contacts using Marshal.ReleaseCOMObject.
Setting the object to null won'r help since the actual call to
IUnknown::Release() happens much later when the GC runs.
Calling GC.Collect() every once in a while is also a good idea.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Thank you Dmitry for your valuable suggesion.
Actually I was tried releaseCOMObject before, but it was also giving
me the same error.
in Finally block I wrote code like -
'-----------------------------------------------------
Marshal.ReleaseComObject(c)
c = nothing
'-----------------------------------------------------

After that I wrote code like -
'-----------------------------------------------------
Marshal.ReleaseComObject(c)
'-----------------------------------------------------
i.e. not c = nothing in the finally block. etc
But it is always failing on 250th record. Should I try something else?

Thanks again,
Dhananjay
 
Make sure you never use multipel dot notation to avoid implicit variables
created by the compiler and also call GC.Collect().

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Back
Top