C# .NET - Iterating Outlook Contacts

Asked By . .
04-Oct-06 05:02 PM

Hi there!, I have this very strange problem:

I want to iterate through Outlook contacts located in a public folder, but iteration stops randomly giving different error codes, among them these ones: -833601531, -695189499, -625983483, -37732347. Everytime I get the same text message: "The operation failed".

for (int i = 1; i <= mexicoContacts.Items.Count; i++)
{

if (mexicoContacts.Items[i] is Outlook.ContactItem)
{
Outlook.ContactItem contact = (Outlook.ContactItem)mexicoContacts.Items[i];
if (contact.LastName != null)
listBox1.Items.Add(contact.LastName);
}


}

Any Ideas?

Thanks...

InnerException  InnerException

04-Oct-06 05:20 PM
Try checking the InnerException of your exception and see if this will give a more descriptive error message that what you are currently getting.

COMException  COMException

04-Oct-06 05:55 PM

Hi Ron!

Some good news:

I added a try / catch block and now I always logoff from Outlook namespace... error shows me this:

Microsoft Office Outlook
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Phone Usage Reporter.exe at Outlook.ItemsClass.get_Item(Object Index)
at Phone_Usage_Reporter.formMain.linkUpdate_LinkClicked(Object sender, LinkLabelLinkClickedEventArgs e) in C:\Documents and Settings\itsadmin\My Documents\Visual Studio 2005\Projects\Toolbox\Phone Usage Reporter\Phone Usage Reporter\Form1.cs:line 39

and now it always reads 164 items... no more...

0-Based  0-Based

04-Oct-06 05:59 PM

In C#, indexes are 0-based.  Given this, try changing the ones in red:

for (int i = 0; i < mexicoContacts.Items.Count; i++)
{

if (mexicoContacts.Items[i] is Outlook.ContactItem)
{
Outlook.ContactItem contact = (Outlook.ContactItem)mexicoContacts.Items[i];
if (contact.LastName != null)
listBox1.Items.Add(contact.LastName);
}


}

166 but no 165  166 but no 165
04-Oct-06 06:07 PM

I tried, but seems Outlook has a 1-based array system... item 0 does not exist... I have something else: I can read item 166 but can't read item 165... strange... that makes me think it's something related to the way the item was saved...

Skip Erroneous Items  Skip Erroneous Items
04-Oct-06 06:10 PM
Looks like there's something wrong with that item.  I suggest that you skip those items that gives you an error and just continue with the items after that and just report which ones are causing the problem.
Sounds easy but...  Sounds easy but...
04-Oct-06 06:24 PM

hehe, sounds easy, but I don't know how to skip this... it seems like Outlook does not have an order in this numbers... :-)

maybe doing a foreach, but how would you do it?

thanks

try/catch inside for loop  try/catch inside for loop
04-Oct-06 06:26 PM

One way to do it is to put your try/catch inside the for loop instead of outside:

for (int i = 1; i <= mexicoContacts.Items.Count; i++) {

try {

if (mexicoContacts.Items[i] is Outlook.ContactItem) {

Outlook.ContactItem contact = (Outlook.ContactItem)mexicoContacts.Items[i];
if (contact.LastName != null)
listBox1.Items.Add(contact.LastName);
}

}

catch (Exception exception) {

}


}

Hey!!!  Hey!!!
04-Oct-06 06:33 PM

You're the man!!! I got all of them! got a big bunch of this:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Phone Usage Reporter.exe

but there they are... I also changed for loop to this:

int total = mexicoContacts.Items.Count;

for (int i = 1; i <= total; i++)

because asking many times for mexicoContacts.Items.Count's value also raised an exception... do you any idea why this happened?

anyway it works now... thanks!!!!

Create New Account
help
VirtualTourUrl> http: / / www.teresablanco.com / Listing / VirtualTour.aspx?ListingID = 958035< / VirtualTourUrl> <Location> <GeoArea id = "207550" type = "Neighborhood" name = "Midtown" / > <GeoArea id = "44569" type = "City" name = "Atlanta" / > <GeoArea id = "289" type = "State" name = "Georgia" abbreviation = "GA" / > <GeoArea id = "44" type = "Country" name = "United States" abbreviation = "US" / > <Address publiclyVisible = "true" suite = "1505"> 120 Ralph McGill Blvd VirtualTourUrl> http: / / www.brooklynsprimeproperties.com / Listing / VirtualTour.aspx?ListingID = 958041< / VirtualTourUrl> <Location> <GeoArea id = "224934" type = "Neighborhood" name = "Georgetown" / > <GeoArea id = "223941" type = "Borough" name = "Brooklyn" / > <GeoArea id = "98054" type = "City" name = "City of Greater New York" / > <GeoArea id = "517" type = "State" name = "New York" abbreviation = "NY" / > <GeoArea id = "44" type = "Country" name = "United States" abbreviation
Text+"'< / h1> " + "This mail contains an attachments"; / / objMsg.Attachments.Add(objAttachment); SmtpMail.Send(objMsg); } catch(Exception err) { Response.Write(err.Message); } Could you please tell me what wrong with 'em. Thank you! Anil find out what the true exception is (the innerException) with code like so: try { SmtpMail.Send( mail ); } catch Exception ex ) { Response.Write("The following exception occurred: " + ex.ToString() ); / / check the InnerException while( ex.InnerException ! = null ) { Response.Write("- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); Response.Write("The following InnerException reported: " + ex.InnerException.ToString() ); ex = ex.InnerException; } } Usually this occurs when no smtp server has
net with Crystal Report 8 5 PLEASE HELP. . . . . . . . . . . . I am getting below error "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in axinterop.crystal.dll Additional information: Exception from HRESULT: 0x800A501B." I am using Crystal report control in VB.net window form, AxCrystalReport1 problem!!! Did you get a fix??? I Have this Error plz help me?????????? An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in axinterop.crystal.dll Additional information: Exception from
errors with: "Interface not registered" Which QuickWatches as: e {"Interface not registered \ r \ n" } System.Exception + [System.Runtime.InteropServices.COMException] {System.Runtime.InteropServices.COMException} System.Runtime.InteropServices.COMException System.Object {System.Runtime.InteropServices.COMException} System.Object _className null