| Iterating Outlook Contacts |
| . . posted at Wednesday, October 04, 2006 5: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 |
| F Cali replied to . . at Wednesday, October 04, 2006 5: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 |
| . . replied to F Cali at Wednesday, October 04, 2006 5: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 |
| F Cali replied to . . at Wednesday, October 04, 2006 5: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 |
| . . replied to F Cali at Wednesday, October 04, 2006 6: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 |
| F Cali replied to . . at Wednesday, October 04, 2006 6: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... |
| . . replied to F Cali at Wednesday, October 04, 2006 6: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 |
| F Cali replied to . . at Wednesday, October 04, 2006 6: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!!! |
| . . replied to F Cali at Wednesday, October 04, 2006 6: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!!!! |
 |
| |
|
|