Looping through Outlook Contacts gives me a 'Invalid Cast Exception'

C

Chris Thunell

I'm trying to loop through an exchange public folder contact list, get some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.invalid cast exception" and "specified cast is not valid". Once i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed out
different things i've tried.

Any thoughts?
Chris Thunell
(e-mail address removed)

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount & "."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing
 
K

Ken Tucker [MVP]

Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

oNS.Logon("YourProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module



Ken

-------------------


I'm trying to loop through an exchange public folder contact list, get some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.invalid cast exception" and "specified cast is not valid". Once i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed out
different things i've tried.

Any thoughts?
Chris Thunell
(e-mail address removed)

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount & "."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing
 
C

Chris Thunell

Still get an error (specified cast is not valid - Invalid cast exception) on
the following code:
oCT = DirectCast(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
(e-mail address removed)
 
K

Ken Tucker [MVP]

Hi,

I get the same error with one of the items in my contacts. That is
the reason for the try catch block in my sample.

Ken
------------------
Still get an error (specified cast is not valid - Invalid cast exception) on
the following code:
oCT = DirectCast(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

And the reason that it happens is that a folder is not required to contain homogeneous items. Contacts folders in particular usually also contain distribution lists. You may want to use Items.Restrict to get only items where the message class is IPM.Contact.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
C

Chris Thunell

Sue,
I'm having trouble with the items.RESTRICT("IPM.Contact") code... how do you
implement it?
Chris

And the reason that it happens is that a folder is not required to contain
homogeneous items. Contacts folders in particular usually also contain
distribution lists. You may want to use Items.Restrict to get only items
where the message class is IPM.Contact.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

Sue Mosher [MVP-Outlook]

Did you read the Help topic on the Restrict method? You need to specify what field to look in as well as what value to look for:

strFind = "[MessageClass] = " & Chr(34) & "IPM.Contact" & Chr(34)
Set colItems = = myFolder.Items.Restrict(strFind)

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Chris Thunell said:
Sue,
I'm having trouble with the items.RESTRICT("IPM.Contact") code... how do you
implement it?
Chris

And the reason that it happens is that a folder is not required to contain
homogeneous items. Contacts folders in particular usually also contain
distribution lists. You may want to use Items.Restrict to get only items
where the message class is IPM.Contact.
 
C

Chris Thunell

Sue, I will try that. Thank you so much for your help! You, your books,
and your website have been a tremendous resource to me over the years.
Keep up the great work!
Thanks again!
Chris Thunell
(e-mail address removed)

Did you read the Help topic on the Restrict method? You need to specify what
field to look in as well as what value to look for:

strFind = "[MessageClass] = " & Chr(34) & "IPM.Contact" & Chr(34)
Set colItems = = myFolder.Items.Restrict(strFind)

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Chris Thunell said:
Sue,
I'm having trouble with the items.RESTRICT("IPM.Contact") code... how do
you
implement it?
Chris

And the reason that it happens is that a folder is not required to contain
homogeneous items. Contacts folders in particular usually also contain
distribution lists. You may want to use Items.Restrict to get only items
where the message class is IPM.Contact.
 

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