I am looping through a Public Folder in our Exchange 2003 server system and
i keep getting invalid cast exceptions although the Message Class is set
correctly for each item.
It works until i get to record 250 and then i start getting the cast
exception errors. Once i get one it continues until the last record which
is 3500. It's almost like the connection dies after 250 records. OR even
though the messageclass is "IPM.Contact".. could it be something else in
reality (these contacts have come from an exchange 5.5 server -> to a
exchange 2000 -> and now currently an exchange 2003 server)?
Please review the following code and any thoughts would be greatly
appreciated.
As a side note i have Oulook 2003 in cached exchange mode (connected) to a
Microsoft Exchange 2003 server
Sample Code:
Imports outlook = Microsoft.Office.Interop.Outlook
Imports System.Reflection
'counting info
Dim iCount As Int16
Dim X As Int32 = 1
'outlook setup
Dim oApp As Object = New outlook.Application
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
Dim strFind As String
strFind = "[MessageClass] = " & Chr(34) & "IPM.Contact" & Chr(34)
Dim oItems As outlook.Items = cContacts.Items.Restrict(strFind)
Dim oCT As outlook.ContactItem
iCount = oItems.Count
For Each oCT In oItems
Try
oCT = DirectCast(oCT, outlook.ContactItem)
Statusbar1.text = "Working on Record " & X
Catch ex As Exception
StatusBar1.Text = "Direct Cast Error at record " & X
End Try
X = X + 1
Next