Exceeds amount of items


M

Magnus

Hello,

I'm trying to go through all contacts, and compare them to all other contacts, to see if they are similar etc etc.
I seems to exceeds the limit of approx 255 open com objects, and I can't figure out how to get my code to work.
As you can see I try to delete the objects, but I can't succeed.

My error which I get after several loops:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.ContactItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063021-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

in the top I have: using ol = Microsoft.Office.Interop.Outlook;
and my procedure simplified:

ol.Application objOutlook = new ol.Application();

ol.MAPIFolder folder = objOutlook.Session.GetDefaultFolder(ol.OlDefaultFolders.olFolderContacts);

ol.Items items = (ol.Items) folder.Items;

int i = 1; int tot = items.Count; int dups = 0;

string dupindexes = "none";

ol.ContactItem[] RemoveItems = new Microsoft.Office.Interop.Outlook.ContactItem[10000];

foreach (ol.ContactItem itm in items)

{

int inclausecount = 0;

ol.MAPIFolder folder2 = objOutlook.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts );

ol.Items items2 = (ol.Items) folder2.Items;

foreach (ol.ContactItem itm2 in items2)

{

inclausecount++;

if (itm.CreationTime <= itm2.CreationTime) goto notmatch;

if (itm.Subject != itm2.Subject) goto notmatch;

dups++;

dupindexes += i.ToString() + ":" + itm.Subject + "\r\n";

itm2.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);

System.Runtime.InteropServices.Marshal.ReleaseComObject(itm2);

GC.Collect();

break;

notmatch:

System.Runtime.InteropServices.Marshal.ReleaseComObject(itm2);

GC.Collect();

}

label1.Text = i.ToString() + "/" + tot.ToString() + ": Dups found: " + dups + "\r\n" + dupindexes;

i++;

System.Runtime.InteropServices.Marshal.ReleaseComObject(itm);

GC.Collect();

}



Please help /Magnus
 
Ad

Advertisements

M

Magnus

Hi Ken,

I'm getting closer. Could you please send me an example of the reflection. I
can't get it work. I believe I try to read a non-contact item?

object obj = folder.Items[j];

ol.ContactItem itm = (ol.ContactItem)obj;

There goes wrong on the second row.

BR /Magnus
 
K

Ken Slovak - [MVP - Outlook]

I use something like this in the NewInspector() event handler. You need a
using statement for System.Reflection in the class:

Outlook.OlObjectClass _class = Outlook.OlObjectClass.olNote;

// set up to get the Class property of the item in the Inspector

object item = Inspector.CurrentItem;

Type _type;

_type = item.GetType();


object[] _args = new Object[] { }; // dummy argument array

try // try to get the Class using reflection

{

_class = (Outlook.OlObjectClass)_type.InvokeMember("Class",

BindingFlags.Public | BindingFlags.GetField |
BindingFlags.GetProperty,

null, item, _args);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

You can then test the Class of the item to see if it's what you want to
handle.
 
M

Magnus

Thank you very much Ken,
Using error handling made my day:)
Regards
/Magnus

Ken Slovak - said:
I use something like this in the NewInspector() event handler. You need a
using statement for System.Reflection in the class:

Outlook.OlObjectClass _class = Outlook.OlObjectClass.olNote;

// set up to get the Class property of the item in the Inspector

object item = Inspector.CurrentItem;

Type _type;

_type = item.GetType();


object[] _args = new Object[] { }; // dummy argument array

try // try to get the Class using reflection

{

_class = (Outlook.OlObjectClass)_type.InvokeMember("Class",

BindingFlags.Public | BindingFlags.GetField |
BindingFlags.GetProperty,

null, item, _args);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

You can then test the Class of the item to see if it's what you want to
handle.




Magnus said:
Hi Ken,

I'm getting closer. Could you please send me an example of the
reflection. I can't get it work. I believe I try to read a non-contact
item?

object obj = folder.Items[j];

ol.ContactItem itm = (ol.ContactItem)obj;

There goes wrong on the second row.

BR /Magnus
 
Ad

Advertisements

K

Ken Slovak - [MVP - Outlook]

The error handling is for failure to get the Class property. If you do get
it you should test it for the OlObjectClass member you want to deal with.
 

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

Similar Threads


Top