Outlook 2003 PIA Question

  • Thread starter Thread starter Richard Waterson
  • Start date Start date
R

Richard Waterson

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
 
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 :))
 
Back
Top