Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other C# .NET Posts   Ask New Question  Ask New Question With Power Editor

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...

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0
InnerException
F Cali provided a rated reply to . . on 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.
Reply    Reply Using Power Editor
How well do you know SQL? Find out with the free test assessment from SQL Server Helper:
http://www.sql-server-helper.com/free-test/default.aspx
  Rank Winnings Points
November 3 $65.00 168
October 0 $0.00 0

COMException
. . replied to F Cali on 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...

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

0-Based
F Cali provided a rated reply to . . on 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);
}


}

Reply    Reply Using Power Editor
How well do you know SQL? Find out with the free test assessment from SQL Server Helper:
http://www.sql-server-helper.com/free-test/default.aspx
  Rank Winnings Points
November 3 $65.00 168
October 0 $0.00 0

166 but no 165
. . replied to F Cali on 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...

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

Skip Erroneous Items
F Cali provided a rated reply to . . on 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.
Reply    Reply Using Power Editor
How well do you know SQL? Find out with the free test assessment from SQL Server Helper:
http://www.sql-server-helper.com/free-test/default.aspx
  Rank Winnings Points
November 3 $65.00 168
October 0 $0.00 0

Sounds easy but...
. . replied to F Cali on 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

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

try/catch inside for loop
F Cali provided a rated reply to . . on 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) {

}


}

Reply    Reply Using Power Editor
How well do you know SQL? Find out with the free test assessment from SQL Server Helper:
http://www.sql-server-helper.com/free-test/default.aspx
  Rank Winnings Points
November 3 $65.00 168
October 0 $0.00 0

Hey!!!
. . replied to F Cali on 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!!!!

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0