473,549 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InvalidCastExce ption with VB .NET 2003 using Outlook 2003

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 oItems" loop. After about 250 contacts, the program gives an InvalidCastExce ption Error on the for each loop. I notice that Outlook's memory keeps increasing (using the task manager) until it reaches around 20,000K. When I run the program a second time (without closing Outlook), I then get the error after only 1 or 2 contacts. If I close Outlook and restart it, the program will run until about 250 contacts. Here is the code

Imports System.Reflecti o
Imports Outlook = Microsoft.Offic e.Interop.Outlo o
Module Module
Sub Main(
Dim i As Intege
Dim StrName As Strin
' Create Outlook application
Dim oApp As Outlook.Applica tion = New Outlook.Applica tio
' Get namespace and Contacts folder reference
Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("MAPI"
Dim cContacts = oNS.Folders.Ite m("Public Folders").
Folders.Item("A ll Public Folders").
Folders.Item("C ontacts"
Dim oItems As Outlook.Items = cContacts.Item
Dim oCt As Outlook.Contact Ite
For each oCt in oItem
Console.Write(C Type(i, String) + " "
oCt = oItems.GetNext(
StrName = CType(oCt.Entry ID, String
Console.WriteLi ne(StrName
oCt = Nothin
Nex
oApp = Nothin
oItems = Nothin
End Su
End Modul

Anyone have any ideas on how to fix this? Right now we have 20,000 contacts in the public folder and the response time using Outlook to find a contact is about 1-2 seconds of search time. I have no problem with Outlook accessing all 20K contacts, only using VB .NET 2003. Any solution would be helpful.

Nov 20 '05 #1
9 3067
I don't know the exactly reason of this. However, set oCt to Nothing is
unnecessary because the object won't get released until GC happens.

One way to make sure you release it is to call
Marshal.Release ComObject(oCt) inside the loop. But you need make sure you
don't use it after you release it.

Thanks
Lifeng
MS VB team

Nov 20 '05 #2
Thanks, but it does look like GC is not running fast enough to keep up. I
added to the bottom of the loop the call to GC to collect and it seems to be
running without running out of memory although it still is gaining a lot
amount of memory during the loop. It is Outlook that is having the problem,
because if I use VBA in Outlook to do the same thing, it crashes the same
way. Only in Outlook I don't have a way to force GC.

George

"Lifeng Lu" <no****@nospam. net> wrote in message
news:zd******** ******@cpmsftng xa10.phx.gbl...
I don't know the exactly reason of this. However, set oCt to Nothing is
unnecessary because the object won't get released until GC happens.

One way to make sure you release it is to call
Marshal.Release ComObject(oCt) inside the loop. But you need make sure you
don't use it after you release it.

Thanks
Lifeng
MS VB team

Nov 20 '05 #3
Yes, It is GC's fault and it exists in Outlook. If I port this code over to
VBA in Outlook and loop through the 20K contacts, Outlook crashes the same
way. Out of memory.... I watched it build up on the task manager to over
42,000K before it crashed. In VB .NET I added the line at the bottom of the
loop to force GC and it looped through without problem, but the memory still
built up more than it should. I don't have a way of forcing GC in Outlook
VBA, so any suggestions? Should I stay with forcing GC in VB .NET? Is the
any way to force GC in VBA?

George

"George McCullen" <te*******@ende rsisland.com> wrote in message
news:23******** *************** ***********@mic rosoft.com...
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 oItems" loop. After
about 250 contacts, the program gives an InvalidCastExce ption Error on the
for each loop. I notice that Outlook's memory keeps increasing (using the
task manager) until it reaches around 20,000K. When I run the program a
second time (without closing Outlook), I then get the error after only 1 or
2 contacts. If I close Outlook and restart it, the program will run until
about 250 contacts. Here is the code:
Imports System.Reflecti on
Imports Outlook = Microsoft.Offic e.Interop.Outlo ok
Module Module1
Sub Main()
Dim i As Integer
Dim StrName As String
' Create Outlook application.
Dim oApp As Outlook.Applica tion = New Outlook.Applica tion
' Get namespace and Contacts folder reference.
Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim cContacts = oNS.Folders.Ite m("Public Folders"). _
Folders.Item("A ll Public Folders"). _
Folders.Item("C ontacts")
Dim oItems As Outlook.Items = cContacts.Items
Dim oCt As Outlook.Contact Item
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
oCt = oItems.GetNext( )
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
oCt = Nothing
Next
oApp = Nothing
oItems = Nothing
End Sub
End Module

Anyone have any ideas on how to fix this? Right now we have 20,000 contacts in the public folder and the response time using Outlook to find a
contact is about 1-2 seconds of search time. I have no problem with Outlook
accessing all 20K contacts, only using VB .NET 2003. Any solution would be
helpful..

Nov 20 '05 #4
Sure wish there was a notice somewhere that explained to people that VB
<> VB.Net and crossposting to both groups makes no sense.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Nov 20 '05 #5
Ken,
You did notice that George was pointing out that OUTLOOK has a problem
whether you use VBA or VB.NET.

What does not make sense is this appears to be more an Outlook problem then
a VB or VB.NET problem...

I will do a little research & see what I can find...

Just a thought
Jay

"Ken Halter" <Ken_Halter@Use _Sparingly_Hotm ail.com> wrote in message
news:Ol******** ******@TK2MSFTN GP11.phx.gbl...
Sure wish there was a notice somewhere that explained to people that VB
<> VB.Net and crossposting to both groups makes no sense.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..

Nov 20 '05 #6
George,
I found the following article on Outlook 2002, I am checking to see if there
is an Outlook 2003 specific version.

http://support.microsoft.com/default...b;en-us;293797

Hope this helps
Jay

"George McCullen" <gm*******@sbcg lobal.net> wrote in message
news:%2******** *********@TK2MS FTNGP11.phx.gbl ...
Yes, It is GC's fault and it exists in Outlook. If I port this code over to VBA in Outlook and loop through the 20K contacts, Outlook crashes the same
way. Out of memory.... I watched it build up on the task manager to over
42,000K before it crashed. In VB .NET I added the line at the bottom of the loop to force GC and it looped through without problem, but the memory still built up more than it should. I don't have a way of forcing GC in Outlook
VBA, so any suggestions? Should I stay with forcing GC in VB .NET? Is the
any way to force GC in VBA?

George

"George McCullen" <te*******@ende rsisland.com> wrote in message
news:23******** *************** ***********@mic rosoft.com...
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 oItems" loop. After
about 250 contacts, the program gives an InvalidCastExce ption Error on the
for each loop. I notice that Outlook's memory keeps increasing (using the
task manager) until it reaches around 20,000K. When I run the program a
second time (without closing Outlook), I then get the error after only 1 or 2 contacts. If I close Outlook and restart it, the program will run until
about 250 contacts. Here is the code:

Imports System.Reflecti on
Imports Outlook = Microsoft.Offic e.Interop.Outlo ok
Module Module1
Sub Main()
Dim i As Integer
Dim StrName As String
' Create Outlook application.
Dim oApp As Outlook.Applica tion = New Outlook.Applica tion
' Get namespace and Contacts folder reference.
Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim cContacts = oNS.Folders.Ite m("Public Folders"). _
Folders.Item("A ll Public Folders"). _
Folders.Item("C ontacts")
Dim oItems As Outlook.Items = cContacts.Items
Dim oCt As Outlook.Contact Item
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
oCt = oItems.GetNext( )
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
oCt = Nothing
Next
oApp = Nothing
oItems = Nothing
End Sub
End Module

Anyone have any ideas on how to fix this? Right now we have 20,000 contacts in the public folder and the response time using Outlook to find

a contact is about 1-2 seconds of search time. I have no problem with Outlook accessing all 20K contacts, only using VB .NET 2003. Any solution would be helpful..


Nov 20 '05 #7
George,
Yes, It is GC's fault and it exists in Outlook. If I port this code over to VBA in Outlook and loop through the 20K contacts, Outlook crashes the same
way. Out of memory.... If it happens in Outlook's VBA, then it has nothing to do with .NET's GC per
se. As Outlook currently does not use .NET internally!

As the KB article I posted last night indicated, this is a known issue in
Outlook. I have confirmed it is still an issue in Outlook 2003. My
understanding is that Outlook does not release the COM objects the for each
is returning until the end of the procedure.

You have a couple of problems with your loop:
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
oCt = oItems.GetNext( )
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
oCt = Nothing
Next
The For Each itself will return the next item in the loop, "oCt =
oItems.GetNext" will return the same item.

Also as Lifeng Lu stated, setting "oCt = Nothing" does exactly that,
Nothing! The for each itself will replace the oCt reference, allowing the GC
to collect the previous item. With COM objects, such as Outlook, you should
use System.Runtime. InteropServices .Marshal.Releas eComObject instead.
ReleaseComObjec t will release Outlook's external reference count on the
item. I understand that Outlook may still have an internal reference count,
hence this helps, but does not fix the problem.

Your loop would be something like:
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
ReleaseComObjec t(oCt)
Next


Alternatives to the OOM (Outlook Object Model), to avoid this problem, would
be to use CDO 1.2.2, Redemption or Extended MAPI.

For a number of resources on using Outlook from .NET see:
http://www.microeye.com/resources/res_outlookvsnet.htm

I recently came across the following http://g8.cx/mapi/ "Extended MAPI with
C#" (I have not yet tried this library). The library should be usable from
VB.NET also!

Hope this helps
Jay

Hope this helps
Jay
"George McCullen" <gm*******@sbcg lobal.net> wrote in message
news:%2******** *********@TK2MS FTNGP11.phx.gbl ... Yes, It is GC's fault and it exists in Outlook. If I port this code over to VBA in Outlook and loop through the 20K contacts, Outlook crashes the same
way. Out of memory.... I watched it build up on the task manager to over
42,000K before it crashed. In VB .NET I added the line at the bottom of the loop to force GC and it looped through without problem, but the memory still built up more than it should. I don't have a way of forcing GC in Outlook
VBA, so any suggestions? Should I stay with forcing GC in VB .NET? Is the
any way to force GC in VBA?

George

"George McCullen" <te*******@ende rsisland.com> wrote in message
news:23******** *************** ***********@mic rosoft.com...
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 oItems" loop. After
about 250 contacts, the program gives an InvalidCastExce ption Error on the
for each loop. I notice that Outlook's memory keeps increasing (using the
task manager) until it reaches around 20,000K. When I run the program a
second time (without closing Outlook), I then get the error after only 1 or 2 contacts. If I close Outlook and restart it, the program will run until
about 250 contacts. Here is the code:

Imports System.Reflecti on
Imports Outlook = Microsoft.Offic e.Interop.Outlo ok
Module Module1
Sub Main()
Dim i As Integer
Dim StrName As String
' Create Outlook application.
Dim oApp As Outlook.Applica tion = New Outlook.Applica tion
' Get namespace and Contacts folder reference.
Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim cContacts = oNS.Folders.Ite m("Public Folders"). _
Folders.Item("A ll Public Folders"). _
Folders.Item("C ontacts")
Dim oItems As Outlook.Items = cContacts.Items
Dim oCt As Outlook.Contact Item
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
oCt = oItems.GetNext( )
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
oCt = Nothing
Next
oApp = Nothing
oItems = Nothing
End Sub
End Module

Anyone have any ideas on how to fix this? Right now we have 20,000 contacts in the public folder and the response time using Outlook to find

a contact is about 1-2 seconds of search time. I have no problem with Outlook accessing all 20K contacts, only using VB .NET 2003. Any solution would be helpful..


Nov 20 '05 #8
Jay B. Harlow [MVP - Outlook] wrote:
Ken,
You did notice that George was pointing out that OUTLOOK has a problem
whether you use VBA or VB.NET.


Doesn't matter. The subject line and all except the last sentence or two
are .Net related and I'll bet that no one has ever had an
'InvalidCastExc eption' error in VB Classic. No one worries about "GC" in
VB Classic. If anything, the crosspost list should've included a VBA group.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Nov 20 '05 #9
I would like to thank all of you for your contributions to solving this
problem. Many things are apparent problems with the Outlook object library.
Memory problems being one of them. Your help is finding work arounds and
other ways to do this loop has been a great help.

I have been able to reduce the amount of memory build up using the
ReleaseComObjec t call and forcing GC to remove the objects.

Thanks,
George

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e3******** *****@TK2MSFTNG P12.phx.gbl...
George,
Yes, It is GC's fault and it exists in Outlook. If I port this code over to
VBA in Outlook and loop through the 20K contacts, Outlook crashes the same
way. Out of memory....

If it happens in Outlook's VBA, then it has nothing to do with .NET's GC

per se. As Outlook currently does not use .NET internally!

As the KB article I posted last night indicated, this is a known issue in
Outlook. I have confirmed it is still an issue in Outlook 2003. My
understanding is that Outlook does not release the COM objects the for each is returning until the end of the procedure.

You have a couple of problems with your loop:
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
oCt = oItems.GetNext( )
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
oCt = Nothing
Next
The For Each itself will return the next item in the loop, "oCt =
oItems.GetNext" will return the same item.

Also as Lifeng Lu stated, setting "oCt = Nothing" does exactly that,
Nothing! The for each itself will replace the oCt reference, allowing the GC to collect the previous item. With COM objects, such as Outlook, you should use System.Runtime. InteropServices .Marshal.Releas eComObject instead.
ReleaseComObjec t will release Outlook's external reference count on the
item. I understand that Outlook may still have an internal reference count, hence this helps, but does not fix the problem.

Your loop would be something like:
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
ReleaseComObjec t(oCt)
Next
Alternatives to the OOM (Outlook Object Model), to avoid this problem, would be to use CDO 1.2.2, Redemption or Extended MAPI.

For a number of resources on using Outlook from .NET see:
http://www.microeye.com/resources/res_outlookvsnet.htm

I recently came across the following http://g8.cx/mapi/ "Extended MAPI with C#" (I have not yet tried this library). The library should be usable from
VB.NET also!

Hope this helps
Jay

Hope this helps
Jay
"George McCullen" <gm*******@sbcg lobal.net> wrote in message
news:%2******** *********@TK2MS FTNGP11.phx.gbl ...
Yes, It is GC's fault and it exists in Outlook. If I port this code over to
VBA in Outlook and loop through the 20K contacts, Outlook crashes the

same way. Out of memory.... I watched it build up on the task manager to over
42,000K before it crashed. In VB .NET I added the line at the bottom of

the
loop to force GC and it looped through without problem, but the memory

still
built up more than it should. I don't have a way of forcing GC in Outlook VBA, so any suggestions? Should I stay with forcing GC in VB .NET? Is the any way to force GC in VBA?

George

"George McCullen" <te*******@ende rsisland.com> wrote in message
news:23******** *************** ***********@mic rosoft.com...
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 oItems" loop. After about 250 contacts, the program gives an InvalidCastExce ption Error on the for each loop. I notice that Outlook's memory keeps increasing (using the task manager) until it reaches around 20,000K. When I run the program a
second time (without closing Outlook), I then get the error after only 1

or
2 contacts. If I close Outlook and restart it, the program will run until about 250 contacts. Here is the code:

Imports System.Reflecti on
Imports Outlook = Microsoft.Offic e.Interop.Outlo ok
Module Module1
Sub Main()
Dim i As Integer
Dim StrName As String
' Create Outlook application.
Dim oApp As Outlook.Applica tion = New Outlook.Applica tion
' Get namespace and Contacts folder reference.
Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim cContacts = oNS.Folders.Ite m("Public Folders"). _
Folders.Item("A ll Public Folders"). _
Folders.Item("C ontacts")
Dim oItems As Outlook.Items = cContacts.Items
Dim oCt As Outlook.Contact Item
For each oCt in oItems
Console.Write(C Type(i, String) + " ")
oCt = oItems.GetNext( )
StrName = CType(oCt.Entry ID, String)
Console.WriteLi ne(StrName)
oCt = Nothing
Next
oApp = Nothing
oItems = Nothing
End Sub
End Module

Anyone have any ideas on how to fix this? Right now we have 20,000

contacts in the public folder and the response time using Outlook to

find a
contact is about 1-2 seconds of search time. I have no problem with

Outlook
accessing all 20K contacts, only using VB .NET 2003. Any solution would

be
helpful..



Nov 20 '05 #10

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

Similar topics

0
4650
by: Nitec Dev | last post by:
Our setup: server1 ASP database running on IIS5, Outlook 2000 and SQL 2000 on Windows 2000. Server2 runs Windows 2000 with Exchange 2000 using CDO 1.2 MAPI Sessions. Clients use IE5/6 and had Outlook 2000 installed. Upgraded to ASP running on IIS6, Outlook 2000 and SQL 2000 on Windows 2003. Server2 runs windows 2003 with Exchange 2003...
14
1669
by: acuttitta | last post by:
Within the next six months, our office is going to be converting it's e-mail server from GroupWise over to Outlook 2003 & MS Exchange. I have been using Dimitri Furman's (google him, he's out there) GroupWise API code for years and have been quite happy with it. However, with the conversion, I'm forced to ditch it for Outlook. The I.T....
4
2649
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am printing these session variables they are printing. After that I am assigning these things in hidden object and in the form submit action I am...
0
623
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is not valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. --->...
4
1580
by: Peter van der Veen | last post by:
Hi In my VB.net application i got on different times (and not allways) an System.InvalidCastException. The line of code is: objAppointmentItem = objItems.GetFirst Does anyone know what to do???
0
2873
by: fish | last post by:
Hi, I have a VB.net application that will save attachments to a directory on my local pc. I need to run this component on our exchange 2003 server and also save the attachments to a local DIR. When the component is run on the exchange 2003 server a wizard gui is automatically started which wants to install Outlook 2003. The component is...
11
4460
by: Maximus | last post by:
Hi all, Has anyone been able to reterive the gradient selection colors used in outlook 2003? Basically, I have a grid showing a list of records and my client wants the selected row to have the same selection gradient back color as Outlook 2003 has on its left pane. The selected pane in Outlook 2003 have a gradient orange color for blue...
1
2848
by: allbelonging | last post by:
C#.Net Outlook 2003 automation (programmatically) with Office.Interop.Outlook Problem: I have my outlook 2003 configured with multiple mailbox on my local machine. I want to specify the mailbox and server (Exchange server mail box) to connect and then save the mailitems(from Inbox or any other folder) based on a filter to a*.msg file. I...
8
4946
by: Joe HM | last post by:
Hello - I was wondering that the "cleanest" way is to determine whether a CType() will throw an InvalidCastException? I have data I receive as an Object and I want to convert it to a String whenever possible using CType(lObjectDummy, String) otherwise I will just ignore it. I could put a Try/Catch As System.InvalidCastException around it...
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7492
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3510
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1957
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.