Outlook 2003 PIA Question

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
 
P

Peter Jausovec

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

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