Connecting Tech Pros Worldwide Help | Site Map

InvalidCastException with VB .NET 2003 using Outlook 2003

George McCullen
Guest
 
Posts: n/a
#1: Nov 20 '05
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 InvalidCastException 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.Reflectio
Imports Outlook = Microsoft.Office.Interop.Outloo
Module Module
Sub Main(
Dim i As Intege
Dim StrName As Strin
' Create Outlook application
Dim oApp As Outlook.Application = New Outlook.Applicatio
' Get namespace and Contacts folder reference
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI"
Dim cContacts = oNS.Folders.Item("Public Folders").
Folders.Item("All Public Folders").
Folders.Item("Contacts"
Dim oItems As Outlook.Items = cContacts.Item
Dim oCt As Outlook.ContactIte
For each oCt in oItem
Console.Write(CType(i, String) + " "
oCt = oItems.GetNext(
StrName = CType(oCt.EntryID, String
Console.WriteLine(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.

Lifeng Lu
Guest
 
Posts: n/a
#2: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


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.ReleaseComObject(oCt) inside the loop. But you need make sure you
don't use it after you release it.

Thanks
Lifeng
MS VB team

George McCullen
Guest
 
Posts: n/a
#3: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


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" <nobody@nospam.net> wrote in message
news:zdfGwVyTEHA.3328@cpmsftngxa10.phx.gbl...[color=blue]
> 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.ReleaseComObject(oCt) inside the loop. But you need make sure you
> don't use it after you release it.
>
> Thanks
> Lifeng
> MS VB team
>[/color]


George McCullen
Guest
 
Posts: n/a
#4: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


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" <technical@endersisland.com> wrote in message
news:236DF508-DD73-4DAD-9D29-41B380E6E14C@microsoft.com...[color=blue]
> I have an Outlook 2003 using Exchange Server 2003 Public Contacts[/color]
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 InvalidCastException 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:[color=blue]
>
> Imports System.Reflection
> Imports Outlook = Microsoft.Office.Interop.Outlook
> Module Module1
> Sub Main()
> Dim i As Integer
> Dim StrName As String
> ' Create Outlook application.
> Dim oApp As Outlook.Application = New Outlook.Application
> ' Get namespace and Contacts folder reference.
> Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
> Dim cContacts = oNS.Folders.Item("Public Folders"). _
> Folders.Item("All Public Folders"). _
> Folders.Item("Contacts")
> Dim oItems As Outlook.Items = cContacts.Items
> Dim oCt As Outlook.ContactItem
> For each oCt in oItems
> Console.Write(CType(i, String) + " ")
> oCt = oItems.GetNext()
> StrName = CType(oCt.EntryID, String)
> Console.WriteLine(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[/color]
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..[color=blue]
>[/color]


Ken Halter
Guest
 
Posts: n/a
#5: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


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..
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#6: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


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_Hotmail.com> wrote in message
news:OlL92VzTEHA.1508@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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..[/color]


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#7: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


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" <gmccullen@sbcglobal.net> wrote in message
news:%23tma%23yyTEHA.544@TK2MSFTNGP11.phx.gbl...[color=blue]
> Yes, It is GC's fault and it exists in Outlook. If I port this code over[/color]
to[color=blue]
> 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[/color]
the[color=blue]
> loop to force GC and it looped through without problem, but the memory[/color]
still[color=blue]
> 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" <technical@endersisland.com> wrote in message
> news:236DF508-DD73-4DAD-9D29-41B380E6E14C@microsoft.com...[color=green]
> > I have an Outlook 2003 using Exchange Server 2003 Public Contacts[/color]
> Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program[/color]
that[color=blue]
> loops through all the contacts in a "for each oCt in oItems" loop. After
> about 250 contacts, the program gives an InvalidCastException 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[/color]
or[color=blue]
> 2 contacts. If I close Outlook and restart it, the program will run until
> about 250 contacts. Here is the code:[color=green]
> >
> > Imports System.Reflection
> > Imports Outlook = Microsoft.Office.Interop.Outlook
> > Module Module1
> > Sub Main()
> > Dim i As Integer
> > Dim StrName As String
> > ' Create Outlook application.
> > Dim oApp As Outlook.Application = New Outlook.Application
> > ' Get namespace and Contacts folder reference.
> > Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
> > Dim cContacts = oNS.Folders.Item("Public Folders"). _
> > Folders.Item("All Public Folders"). _
> > Folders.Item("Contacts")
> > Dim oItems As Outlook.Items = cContacts.Items
> > Dim oCt As Outlook.ContactItem
> > For each oCt in oItems
> > Console.Write(CType(i, String) + " ")
> > oCt = oItems.GetNext()
> > StrName = CType(oCt.EntryID, String)
> > Console.WriteLine(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[/color]
> contacts in the public folder and the response time using Outlook to find[/color]
a[color=blue]
> contact is about 1-2 seconds of search time. I have no problem with[/color]
Outlook[color=blue]
> accessing all 20K contacts, only using VB .NET 2003. Any solution would[/color]
be[color=blue]
> helpful..[color=green]
> >[/color]
>
>[/color]


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#8: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


George,[color=blue]
> Yes, It is GC's fault and it exists in Outlook. If I port this code over[/color]
to[color=blue]
> VBA in Outlook and loop through the 20K contacts, Outlook crashes the same
> way. Out of memory....[/color]
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:
[color=blue][color=green]
> > For each oCt in oItems
> > Console.Write(CType(i, String) + " ")
> > oCt = oItems.GetNext()
> > StrName = CType(oCt.EntryID, String)
> > Console.WriteLine(StrName)
> > oCt = Nothing
> > Next[/color][/color]

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.ReleaseComO bject instead.
ReleaseComObject 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:
[color=blue][color=green]
> > For each oCt in oItems
> > Console.Write(CType(i, String) + " ")
> > StrName = CType(oCt.EntryID, String)
> > Console.WriteLine(StrName)
> > ReleaseComObject(oCt)
> > Next[/color][/color]

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" <gmccullen@sbcglobal.net> wrote in message
news:%23tma%23yyTEHA.544@TK2MSFTNGP11.phx.gbl...[color=blue]
> Yes, It is GC's fault and it exists in Outlook. If I port this code over[/color]
to[color=blue]
> 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[/color]
the[color=blue]
> loop to force GC and it looped through without problem, but the memory[/color]
still[color=blue]
> 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" <technical@endersisland.com> wrote in message
> news:236DF508-DD73-4DAD-9D29-41B380E6E14C@microsoft.com...[color=green]
> > I have an Outlook 2003 using Exchange Server 2003 Public Contacts[/color]
> Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program[/color]
that[color=blue]
> loops through all the contacts in a "for each oCt in oItems" loop. After
> about 250 contacts, the program gives an InvalidCastException 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[/color]
or[color=blue]
> 2 contacts. If I close Outlook and restart it, the program will run until
> about 250 contacts. Here is the code:[color=green]
> >
> > Imports System.Reflection
> > Imports Outlook = Microsoft.Office.Interop.Outlook
> > Module Module1
> > Sub Main()
> > Dim i As Integer
> > Dim StrName As String
> > ' Create Outlook application.
> > Dim oApp As Outlook.Application = New Outlook.Application
> > ' Get namespace and Contacts folder reference.
> > Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
> > Dim cContacts = oNS.Folders.Item("Public Folders"). _
> > Folders.Item("All Public Folders"). _
> > Folders.Item("Contacts")
> > Dim oItems As Outlook.Items = cContacts.Items
> > Dim oCt As Outlook.ContactItem
> > For each oCt in oItems
> > Console.Write(CType(i, String) + " ")
> > oCt = oItems.GetNext()
> > StrName = CType(oCt.EntryID, String)
> > Console.WriteLine(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[/color]
> contacts in the public folder and the response time using Outlook to find[/color]
a[color=blue]
> contact is about 1-2 seconds of search time. I have no problem with[/color]
Outlook[color=blue]
> accessing all 20K contacts, only using VB .NET 2003. Any solution would[/color]
be[color=blue]
> helpful..[color=green]
> >[/color]
>
>[/color]


Ken Halter
Guest
 
Posts: n/a
#9: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


Jay B. Harlow [MVP - Outlook] wrote:[color=blue]
> Ken,
> You did notice that George was pointing out that OUTLOOK has a problem
> whether you use VBA or VB.NET.[/color]

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
'InvalidCastException' 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..
George McCullen
Guest
 
Posts: n/a
#10: Nov 20 '05

re: InvalidCastException with VB .NET 2003 using Outlook 2003


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
ReleaseComObject call and forcing GC to remove the objects.

Thanks,
George

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:e3El$r7TEHA.156@TK2MSFTNGP12.phx.gbl...[color=blue]
> George,[color=green]
> > Yes, It is GC's fault and it exists in Outlook. If I port this code over[/color]
> to[color=green]
> > VBA in Outlook and loop through the 20K contacts, Outlook crashes the[/color][/color]
same[color=blue][color=green]
> > way. Out of memory....[/color]
> If it happens in Outlook's VBA, then it has nothing to do with .NET's GC[/color]
per[color=blue]
> 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[/color]
each[color=blue]
> is returning until the end of the procedure.
>
> You have a couple of problems with your loop:
>[color=green][color=darkred]
> > > For each oCt in oItems
> > > Console.Write(CType(i, String) + " ")
> > > oCt = oItems.GetNext()
> > > StrName = CType(oCt.EntryID, String)
> > > Console.WriteLine(StrName)
> > > oCt = Nothing
> > > Next[/color][/color]
>
> 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[/color]
GC[color=blue]
> to collect the previous item. With COM objects, such as Outlook, you[/color]
should[color=blue]
> use System.Runtime.InteropServices.Marshal.ReleaseComO bject instead.
> ReleaseComObject will release Outlook's external reference count on the
> item. I understand that Outlook may still have an internal reference[/color]
count,[color=blue]
> hence this helps, but does not fix the problem.
>
> Your loop would be something like:
>[color=green][color=darkred]
> > > For each oCt in oItems
> > > Console.Write(CType(i, String) + " ")
> > > StrName = CType(oCt.EntryID, String)
> > > Console.WriteLine(StrName)
> > > ReleaseComObject(oCt)
> > > Next[/color][/color]
>
> Alternatives to the OOM (Outlook Object Model), to avoid this problem,[/color]
would[color=blue]
> 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[/color]
with[color=blue]
> 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" <gmccullen@sbcglobal.net> wrote in message
> news:%23tma%23yyTEHA.544@TK2MSFTNGP11.phx.gbl...[color=green]
> > Yes, It is GC's fault and it exists in Outlook. If I port this code over[/color]
> to[color=green]
> > VBA in Outlook and loop through the 20K contacts, Outlook crashes the[/color][/color]
same[color=blue][color=green]
> > 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[/color]
> the[color=green]
> > loop to force GC and it looped through without problem, but the memory[/color]
> still[color=green]
> > built up more than it should. I don't have a way of forcing GC in[/color][/color]
Outlook[color=blue][color=green]
> > VBA, so any suggestions? Should I stay with forcing GC in VB .NET? Is[/color][/color]
the[color=blue][color=green]
> > any way to force GC in VBA?
> >
> > George
> >
> > "George McCullen" <technical@endersisland.com> wrote in message
> > news:236DF508-DD73-4DAD-9D29-41B380E6E14C@microsoft.com...[color=darkred]
> > > I have an Outlook 2003 using Exchange Server 2003 Public Contacts[/color]
> > Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program[/color]
> that[color=green]
> > loops through all the contacts in a "for each oCt in oItems" loop.[/color][/color]
After[color=blue][color=green]
> > about 250 contacts, the program gives an InvalidCastException Error on[/color][/color]
the[color=blue][color=green]
> > for each loop. I notice that Outlook's memory keeps increasing (using[/color][/color]
the[color=blue][color=green]
> > 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[/color]
> or[color=green]
> > 2 contacts. If I close Outlook and restart it, the program will run[/color][/color]
until[color=blue][color=green]
> > about 250 contacts. Here is the code:[color=darkred]
> > >
> > > Imports System.Reflection
> > > Imports Outlook = Microsoft.Office.Interop.Outlook
> > > Module Module1
> > > Sub Main()
> > > Dim i As Integer
> > > Dim StrName As String
> > > ' Create Outlook application.
> > > Dim oApp As Outlook.Application = New Outlook.Application
> > > ' Get namespace and Contacts folder reference.
> > > Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
> > > Dim cContacts = oNS.Folders.Item("Public Folders"). _
> > > Folders.Item("All Public Folders"). _
> > > Folders.Item("Contacts")
> > > Dim oItems As Outlook.Items = cContacts.Items
> > > Dim oCt As Outlook.ContactItem
> > > For each oCt in oItems
> > > Console.Write(CType(i, String) + " ")
> > > oCt = oItems.GetNext()
> > > StrName = CType(oCt.EntryID, String)
> > > Console.WriteLine(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[/color]
> > contacts in the public folder and the response time using Outlook to[/color][/color]
find[color=blue]
> a[color=green]
> > contact is about 1-2 seconds of search time. I have no problem with[/color]
> Outlook[color=green]
> > accessing all 20K contacts, only using VB .NET 2003. Any solution would[/color]
> be[color=green]
> > helpful..[color=darkred]
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread