Problem with "Outlook-Contacts"

  • Thread starter Thread starter Corey66
  • Start date Start date
C

Corey66

Hello!

I think, I have one simple question for you guys, but I don't get the answer
by myself!

I would like to import all "Outlook - Contacts" into my application and when
I use "foreach" it works! But with "Outlook 2002" I must use a "for - Loop"
and that is my problem! I have three contacts in Outlook and when I import
the "contacts", it gives me everytime only two contacts!

Here is my "for - loop" and I hope one of you could find my (simple) mistake!

*********
int zaehler;

object item = (MSOutlook.ContactItem)objFolder.Items.GetFirst();

for (zaehler = 0; zaehler <= objFolder.Items.Count; zaehler++)
{
appItem = item as MSOutlook.ContactItem;

if (appItem != null)
{
importContactItems.Tables[0].Rows.Add(new object[] {
appItem.LastName, appItem.FirstName, appItem.
HomeAddressStreet,
appItem.HomeAddressPostalCode, appItem.HomeAddressCity});
item = (MSOutlook.ContactItem)objFolder.Items.GetNext();
zaehler++;
}
}
**********

Regards

Corey

P.S. Sry, for my bad english! *g
 
Hi Corey,
for (zaehler = 0; zaehler <= objFolder.Items.Count; zaehler++)
{
zaehler++;

There's your problem: You increment zaehler within the for loop, but the
for loop itself increments already.

Hope this helps,

Cheers,

Roland
 
Thx for your help!

But when I have three Contacts, he shows me that the three Contacts exists,
but he I don't get the last one!
He shows me the first two Contacts in my table, but the third record is my
second Contact again! :-(

My new Code:

int zaehler;

object item = (MSOutlook.ContactItem)objFolder.Items.GetFirst();

// Get the number of my "Contacts"
MessageBox.Show(objFolder.Items.Count.ToString());

MSOutlook.ContactItem appItem

for (zaehler = 0; zaehler <= objFolder.Items.Count-1; zaehler++)
{
appItem = item as MSOutlook.ContactItem;

if (appItem != null)
{
importContactItems.Tables[0].Rows.Add(new object[] {
appItem.LastName, appItem.FirstName, appItem.HomeAddressStreet,
appItem.HomeAddressPostalCode, appItem.HomeAddressCity});

item = (MSOutlook.ContactItem)objFolder.Items.GetNext();
}
}
 
Can nobody help me? :-(

Regards

Corey

Why don't you try using a foreach loop instead. It will be cleaner:

// Get the number of my "Contacts"
MessageBox.Show(objFolder.Items.Count.ToString());

foreach (MSOutlook.ContactItem appItem in objFolder.Items)
{

if (appItem != null)
{
importContactItems.Tables[0].Rows.Add(new object[] {
appItem.LastName, appItem.FirstName, appItem.HomeAddressStreet,
appItem.HomeAddressPostalCode, appItem.HomeAddressCity});
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top