I have some code that loops through all contacts in a contact folder (approx 3000), reads some details from each contact and uses that information to perform various functions.
However, as the program loops through, reading each contact, it will randomly start throwing exceptions (usually a large batch at a time), therefore fails to read the contact. This is the current code snippet that I am using, although I have tried allsorts. I can't seem to get around some erratic (i.e. crap) behaviour from Outlook.
The exception thrown is "Specified cast is not valid" and occurs in the first statement in the "try" (well as you can see here, that is currently the ONLY statement in the "try" block!!).
I think it seems to think the object is an Outlook.__ComObject... or something like that..
Please can someone help, I am losing my hair here!! This is using Outlook 2003, developing with Visual Studio 2003 and C# .net!
Outlook.Application olApp = new Outlook.ApplicationClass();
Outlook.NameSpace NS = olApp.GetNamespace("mapi");
Outlook.MAPIFolder oContacts = NS.GetFolderFromID(ContactFolderEntryID,FolderStor eID);
Outlook.Items oItems = (Outlook.Items)oContacts.Items;
for(int i=1; i<=oItems.Count;i++)
{
try
{
Outlook.ContactItem oCt = (Outlook.ContactItem)oItems.GetNext();
//MessageBox.Show(oCt.FirstName);
//MessageBox.Show(oCt.CustomerID);
/*string CustomerID = oCt.CustomerID;
if(Array.BinarySearch(UpdatedContactJustIDs,Custom erID)>=0)
{
}
else
{
//see if it is on OCRM
OutlookDeleteCount++;
}*/
}
catch(Exception ex)
{
MessageBox.Show(i.ToString());
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
}