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]