Help!

Outlook 2003 PIA Question


Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Interoperability RSS
Next:  Outlook 2000 reinstall  
Author Message
kwc5811
External


Since: Jan 23, 2007
Posts: 1



PostPosted: Tue Jan 23, 2007 6:44 am    Post subject: Outlook 2003 PIA Question
Archived from groups: microsoft>public>outlook>interop (more info?)

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.
Back to top
Brian Tillman
External


Since: Mar 29, 2005
Posts: 20862



PostPosted: Tue Jan 23, 2007 4:49 pm    Post subject: Re: Outlook 2003 PIA Question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

kwc5811.TakeThisOut@gmail.com <kwc5811.TakeThisOut@gmail.com> wrote:

> 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
--
Brian Tillman
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Interoperability All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum