System._ComObject problem

R

Romeo

I have the following function which is giving me a headache...As you can see,
if pItem is "MailItem" or a "PostItem", cast it to the appropriate type.

Public Shared Function Create(ByVal pItem As Object) As OutlookItem
If TypeName(pItem) = "MailItem" Then
Return New MailItem(CType(pItem,
Microsoft.Office.Interop.Outlook.MailItem))
ElseIf TypeName(pItem) = "PostItem" Then
Return New
PostItem(CType(pItem,Microsoft.Office.Interop.Outlook.PostItem))
ElseIf Not (TypeName(pItem) = "PostItem" OrElse TypeName(pItem) =
"MailItem") Then
Throw New ApplicationException("The Outlook item type is not
supported by this application.")
Else
Return Nothing
End If
End Function

The problem is I am getting the following InvalidCastException:

Unable to cast COM object of type 'System.__ComObject' to interface type
'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because
the QueryInterface call on the COM component for the interface with IID
'{00063034-0000-0000-C000-000000000046}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).

Why is it telling me that I cannot cast pItem because it is a
"System._ComObject"? Isn't pItem a "MailItem" which is what the TypeName
function is checking for??

Any help with this would be appreciated. Thanks.

Romeo Chua
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top