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
Peter Jausovec - 28 Dec 2004 09:25 GMT
Hi,
You should cast the object type to the ItemsClass and ContactItemClass where
is neccessary. (I am from C# background; don't know how the thing is done in
VB.NET :))

Signature
Regards,
Peter Jausovec
(http://blog.jausovec.net)
>I am trying to run this sample code but keep getting an "Invalid Cast"
> exception when calling oItems.GetFirst and assigning the resulting
[quoted text clipped - 35 lines]
> oItems = Nothing
> oCt = Nothing