to many open objects

A

Anonymous

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace PrintContacts
{
class Program
{
static void Main(string[] args)
{
Application outlook = new Application();
NameSpace ns = outlook.GetNamespace("MAPI");
MAPIFolder cf = ns.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
foreach (object o in cf.Items)
{
if ((o as ContactItem) != null)
{
ContactItem ci = (ContactItem)o;
Console.WriteLine(ci.FullName);
}
}
}
}
}

--> After listed about hundred contacts I get the error:
System.Runtime.InteropServices.COMException (0xB5940305): [localized
message - something like "to many open objects on the exchange server"].
vid Microsoft.Office.Interop.Outlook._ContactItem.get_FullName()
vid PrintContacts.Program.Main(String[] args) i
C:\PrintContacts\Program.cs

How can I prevent this?
 
B

Brian Tillman [MVP-Outlook]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;

Please ask programming questions in microsoft.public.outlook.program_addins or
..program_vba, as appropriate.
 
K

Ken Slovak - [MVP - Outlook]

You are probably running into the limit of 255 RPC connections to Exchange.
You must release objects in your loop so you don't hit that limit. You are
best off using a for loop and not a foreach as that gives you control of
your objects and when they are released. Declare the objects outside the
loop so you aren't creating too many objects, then release them inside the
loop.
 
A

Anonymous

How do I release a ContactItem?

Ken Slovak - said:
You are probably running into the limit of 255 RPC connections to
Exchange. You must release objects in your loop so you don't hit that
limit. You are best off using a for loop and not a foreach as that gives
you control of your objects and when they are released. Declare the
objects outside the loop so you aren't creating too many objects, then
release them inside the loop.




Anonymous said:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace PrintContacts
{
class Program
{
static void Main(string[] args)
{
Application outlook = new Application();
NameSpace ns = outlook.GetNamespace("MAPI");
MAPIFolder cf = ns.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
foreach (object o in cf.Items)
{
if ((o as ContactItem) != null)
{
ContactItem ci = (ContactItem)o;
Console.WriteLine(ci.FullName);
}
}
}
}
}

--> After listed about hundred contacts I get the error:
System.Runtime.InteropServices.COMException (0xB5940305): [localized
message - something like "to many open objects on the exchange server"].
vid Microsoft.Office.Interop.Outlook._ContactItem.get_FullName()
vid PrintContacts.Program.Main(String[] args) i
C:\PrintContacts\Program.cs

How can I prevent this?
 
K

Ken Slovak - [MVP - Outlook]

To release any object you set it to Nothing or null. You may also need to
call Marshal.ReleaseComObject() on it and then explicitly 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