473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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


Nov 21 '05 #1
3 4489
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:OY**************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #2
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
ct******@pierceassociates.com
"Ken Slovak - [MVP - Outlook]" <ke*******@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:OY**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #3
Set the objects to Nothing, call Marshall.ReleaseCOMObject or whatever it is
and call the garbage collector.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eA**************@TK2MSFTNGP12.phx.gbl...
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
ct******@pierceassociates.com


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fritz Switzer | last post by:
Can anyone provide a small snippet in C# that pulls out the Contacts in Outlook XP. I've seen a couple of examples in C++ and VB in previous newsgroup posts, but either the originals didn't work...
3
by: Kurt | last post by:
Hi We are developing an off-the-shelf software suite for a certain business sector. Most of the program is simply a GUI on top of some .mdb files. Its a .net application written in c# One...
0
by: RBrady | last post by:
When trying to call this class to create an item in a public folder, I get the following error: Server execution failed at VRInterop.Appointment.FindPublicFolder(String folderName) at...
9
by: George McCullen | last post by:
I have an Outlook 2003 using Exchange Server 2003 Public Contacts Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program that loops through all the contacts in a "for each oCt in...
7
by: Chris Thunell | last post by:
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...
2
by: Pieter | last post by:
Hi, I'm using a thight integration with Outlook 2003 (with an Exchange server) in my VB.NET (2005) application. Until now I'm using the Outlook Object Model, but it appears to be very slow, and...
1
by: Phil Stanton | last post by:
I have a Yacht Club Db with names addresse phone nos, emails etc. I want to export them to Outlook. No problem in getting them into the contact folder. My problem is I have a folder within the...
1
by: philmarsay | last post by:
I have some code that loops through all contacts in a contact folder (approx 3000), reads some details from each contact and uses that information to perform various functions. However, as the...
0
by: jazeelkm | last post by:
Hi , I am developing a windows application using C#. In that I want to display all the outlook profiles in a listbox and on selecting one, The folder structure should be displayed on a treeview...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.