Outlook 2003 PIA Question

K

kwc5811

The original question as quoted from Richard Waterson posted on Dec 27,
2004:

I am trying to run this sample code but keep getting an "Invalid Cast"
exception when calling oItems.GetFirst and assigning the resulting
object to a ContactItem. The ItemsClass object has a count of 208
items, so I'm confident the collection contains all my contacts. Where
am I going wrong?

' Create Outlook application.
Dim oApp As Outlook.Application = New
Outlook.Application


' Get namespace and Contacts folder reference.
Dim oNS As Outlook.NameSpace =
oApp.GetNamespace("MAPI")
Dim cContacts As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)


' Get the first contact from the Contacts folder.
Dim oItems As Outlook.ItemsClass = cContacts.Items
Dim oCt As Outlook.ContactItemClass


Dim iCount As Int16
iCount = 0
oCt = oItems.GetFirst()


Do While Not oCt Is Nothing


iCount += 1


Console.WriteLine(oCt.FullName)
oCt = oItems.GetNext


Loop


Console.WriteLine(iCount)


' Clean up.
oApp = Nothing
oItems = Nothing
oCt = Nothing

As posted in an earlier thread, I ran into the same problem. After much
searching there was not a concise answer to the issue. The issue I
discovered was that the sample code types every object in the contacts
folder as a contact. If you have other objects, like a distribution
list, you will run into the useless error message:

Unhandled Exception: System.InvalidCastException: 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)). at OLContacts.Module1.Main() in {your vb
file}

I solved the problem by testing the objects to see what they are before
casting them.

If (TypeOf selObject Is Outlook.ContactItem) Then
Dim contactItem As Outlook.ContactItem =
TryCast(selObject, Outlook.ContactItem)
Console.WriteLine("The item is a contact." & " The full
name is " & _
contactItem.Subject & ".")
ElseIf (TypeOf selObject Is Outlook.DistListItem) Then
Dim distItem As Outlook.DistListItem =
TryCast(selObject, Outlook.DistListItem)
Console.WriteLine("The item is a ditribution list." & "
The list name is " & _
distItem.Subject & ".")
Else
Console.WriteLine("The item is something else")
End If

Hope this helps someone else.
 
B

Brian Tillman

The original question as quoted from Richard Waterson posted on Dec
27, 2004:

I am trying to run this sample code but keep getting an "Invalid Cast"
exception when calling oItems.GetFirst and assigning the resulting
object to a ContactItem. The ItemsClass object has a count of 208
items, so I'm confident the collection contains all my contacts. Where
am I going wrong?

Ask in the programming newsgroup
news://msnews.microsoft.com/microsoft.public.outlook.program_vba
 

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