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