473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Looping through Contact Items in Outlook Throws Exceptions

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);
}
}
Apr 25 '07 #1
1 2161
TRScheel
638 Expert 512MB
Outlook.Items oItems = (Outlook.Items)oContacts.Items;
....
Outlook.ContactItem oCt = (Outlook.ContactItem)oItems.GetNext();
I would wager that not all the oContacts.Items can be cast to ContactItem, ie, they have the same inheritance (Outlook.Items) but are not ContactItem's.

I would do a type check first... instead of doing

Outlook.ContactItem oCT

do

object oCT

and then

oCT.GetType() == typeof(Outlook.ContactItem)

I believe that should work even for inherited items.

Run that check, then continue your code. If it fails, its not castable to ContactItem.
Apr 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chen | last post by:
Are there any sample code for access Contact items in public folder of Outlook from VB.NET Thanks
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
1
by: LeAnne | last post by:
Does anyone know how i could display a MS Outlook screen which can be used for the user to select a contact. I need a screen to display the outlook contacts and for the user to select one of them...
7
by: Wolf | last post by:
Hi all Who can Help me with this one? if I have this Enums: #region Title public enum Title: short { NA = 0,
0
by: Slawomir Nasiadka | last post by:
Hi, I'am new to this group so I would like to say "Hello" everyone and here is my problem: I'm writing a simple application (code is at the end of this message) witch would list all mails...
4
by: kannan | last post by:
Hi, I am new to C# and It may be a simple question. I am trying to loop through MS-Outlook object Contact address list. I can able to find the count using following line. int iAddresscount =...
1
by: kannan | last post by:
Hi, I am trying to loop through outlook contact address and display in messagebox. I have used following code: Outlook._Application olApp = new Outlook.ApplicationClass(); Outlook._NameSpace...
1
by: Kannan | last post by:
Hi, I am writing Outlook Add-in project using C#. I want to loop through calender items from 17-Oct=2006 to 10-Jan-2007. If I found any Meeting request I want to delete that items. Anybody knows...
1
by: mary mathews | last post by:
Hi, I'm getting an exception while tried to retrive an outlook contact using asp.net in a console application. The exception is as follows: An unhandled exception of type...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.