vb.net Outlook Cast Not Valid - Problems with items in Public Folder

C

Chris Thunell

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
 
K

Ken Slovak - [MVP - Outlook]

There is a limit of about 250 open RPC channels when using .NET code with
Outlook and Exchange. That can be changed by the Exchange admin in the
registry of the server or you can explicitly release your objects and
specifically release the object that was marshalled and then call the
garbage collector. That will release the RPC channels so your code can
continue without errors. If you don't do that the channels are locked until
your procedure ends and at some undetermined time the garbage collector runs
on your objects.
 
C

Chris Thunell

Thanks! Looking at my code... how would i release the object? I don't want
to open up 4000 channels if the default is 250 (not sure what the impact
would be).
Chris Thunell
(e-mail address removed)


Ken Slovak - said:
There is a limit of about 250 open RPC channels when using .NET code with
Outlook and Exchange. That can be changed by the Exchange admin in the
registry of the server or you can explicitly release your objects and
specifically release the object that was marshalled and then call the
garbage collector. That will release the RPC channels so your code can
continue without errors. If you don't do that the channels are locked
until your procedure ends and at some undetermined time the garbage
collector runs on your objects.




Chris Thunell said:
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
 
K

Ken Slovak - [MVP - Outlook]

Set the objects to Nothing, call Marshall.ReleaseCOMObject or whatever it is
and call the garbage collector.
 

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