View Single Post
  #2  
Old March 22nd 10, 06:25 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Iterating a Contacts folder for Outlook 2003 and 2007 in C#

A contacts folder can have distribution lists in it, any code that doesn't
handle that possibility will fail in some cases. Also, Outlook collections
are 1 based and don't start at 0. You also should not concatenate dot
operators, that creates invisible object variables you cannot release,
always declare everything explicitly.

Outlook.Items items = oContactFolder.Items;

Outlook.ContactItem c = null;

if (items.Count 0)
{
for (int i = 1; i = items.Count; i++)
{
try
{
c = (Outlook.ContactItem)items[i]; // if DL will error to catch
block
}

etc.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Marketware" wrote in message
...
I need to iterate through all of the contacts in a contact folder. I have
tried two methods to get the contacts out of a Contact folder: Here is
the
first code I've tried:

Outlook.Application Outlook = new Outlook.Application();
Outlook.MAPIFolder fldContacts =
(Outlook.MAPIFolder)Outlook.Session.GetDefaultFold er(OlDefaultFolders.olFolderContacts);

foreach (Outlook.ContactItem oItem1 in fldContacts.Items)
{//Get each contact...}

I get the following complile error when I attempt the above:

foreach statement cannot operate on variables of type 'Outlook.Items'
because 'Outlook.Items' does not contain a public definition for
'GetEnumerator'
Then I tried this approach:

Outlook.ContactItem oItem;

for (int j = 0; j oContactFolder.Items.Count; j++)
{
if (j == 0)
oItem = (Outlook.ContactItem)oContactFolder.Items.GetFirst ();
else
oItem = (Outlook.ContactItem)oContactFolder.Items.GetNext( );

But with the above approach I'm getting the first contact, and a second,
but
from that point on it keeps returning the same second record (out of 20
total).

Can anyone help me see what I am doing wrong here? It really shouldn't be
that difficult to accomplish what I am trying to do here.

Thanks!!!!!!!!!

bob


Ads