A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Iterating a Contacts folder for Outlook 2003 and 2007 in C#



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old March 22nd 10, 04:35 PM posted to microsoft.public.outlook.program_vba
Marketware
external usenet poster
 
Posts: 6
Default Iterating a Contacts folder for Outlook 2003 and 2007 in C#

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


  #3  
Old March 22nd 10, 07:22 PM posted to microsoft.public.outlook.program_vba
Marketware
external usenet poster
 
Posts: 6
Default Iterating a Contacts folder for Outlook 2003 and 2007 in C#

Now I have a new compiler problem. On the line:

c = (Outlook.ContactItem)items[i];

I get the following:

Cannot apply indexing with [] to an expression of type 'Outlook.Items'

I've also tried to replace with () and I get another error about trying to
use as a method.

Ideas??


"Ken Slovak - [MVP - Outlook]" wrote:
[i]
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; // 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


.

  #4  
Old March 22nd 10, 07:40 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#

No idea, that is very bizarre.

I use that type of code all the time. It compiles perfectly here. In fact, I
copied it from a working project I was just compiling.

I just tested and compiled again, and it compiled again with no errors.
There's some other problem that you have.

--
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
news[i]
Now I have a new compiler problem. On the line:

c = (Outlook.ContactItem)items;

I get the following:

Cannot apply indexing with [] to an expression of type 'Outlook.Items'

I've also tried to replace with () and I get another error about trying to
use as a method.

Ideas??


  #5  
Old March 22nd 10, 08:41 PM posted to microsoft.public.outlook.program_vba
Marketware
external usenet poster
 
Posts: 6
Default Iterating a Contacts folder for Outlook 2003 and 2007 in C#

I am using the code you wrote to instaniate an Outlook object. I'm going to
create a new project (separate out just this code) into a new project and see
if I can see anything.

bob

"Ken Slovak - [MVP - Outlook]" wrote:
[i]
No idea, that is very bizarre.

I use that type of code all the time. It compiles perfectly here. In fact, I
copied it from a working project I was just compiling.

I just tested and compiled again, and it compiled again with no errors.
There's some other problem that you have.

--
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
news
Now I have a new compiler problem. On the line:

c = (Outlook.ContactItem)items;

I get the following:

Cannot apply indexing with [] to an expression of type 'Outlook.Items'

I've also tried to replace with () and I get another error about trying to
use as a method.

Ideas??


.

  #6  
Old March 23rd 10, 01:31 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 thought occurred to me. Is this with a PIA from Add-In Express? That might
have a limitation of some sort on the enumerator of an Items collection or
using an indexed operator. The standard PIA's certainly don't have that
limitation, I use Items collections all the time in c# code.

In just looking at an Add-In Express PIA for Outlook I see that it's
exposing an Item object that takes an index value, so that looks like the
problem. Something like items.Item(i) looks like it would work.

--
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 am using the code you wrote to instaniate an Outlook object. I'm going
to
create a new project (separate out just this code) into a new project and
see
if I can see anything.

bob


  #7  
Old March 23rd 10, 11:24 PM posted to microsoft.public.outlook.program_vba
Marketware
external usenet poster
 
Posts: 6
Default Iterating a Contacts folder for Outlook 2003 and 2007 in C#

It appears as if I've got some weird "Using Statements" One that's just

Using Outlook;

and other,

Using Outlook1 = Microsoft.Office.Interop.Outlook;

And the original DLL on the above did not allow the "foreach". I found
another DLL which does allow it, and it seemed to work with both 2003 and
2007 so I think I may be OK with that. But do you know what the first one is
pointing to? If I remove it I get a bunch of errors in the stuff you wrote
for us.


"Marketware" wrote:
[i]
I am using the code you wrote to instaniate an Outlook object. I'm going to
create a new project (separate out just this code) into a new project and see
if I can see anything.

bob

"Ken Slovak - [MVP - Outlook]" wrote:

No idea, that is very bizarre.

I use that type of code all the time. It compiles perfectly here. In fact, I
copied it from a working project I was just compiling.

I just tested and compiled again, and it compiled again with no errors.
There's some other problem that you have.

--
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
news
Now I have a new compiler problem. On the line:

c = (Outlook.ContactItem)items;

I get the following:

Cannot apply indexing with [] to an expression of type 'Outlook.Items'

I've also tried to replace with () and I get another error about trying to
use as a method.

Ideas??


.

  #8  
Old March 24th 10, 10:55 AM posted to microsoft.public.outlook.program_vba
Andrei Smolin [Add-in Express][_2_]
external usenet poster
 
Posts: 5
Default Iterating a Contacts folder for Outlook 2003 and 2007 in C#

Hi Bob,

Using Outlook;


Add-in Express provides version-neutral interops. In your case the interops
are for Outlook 2000 and Office 2000 (common tools). The namespaces
contained in these interops are Outlook and Office, respectively.

Using Outlook1 = Microsoft.Office.Interop.Outlook;


This statement refers to PIA for some Outlook version; it may be Outlook
2002, 2003 or 2007.
[i]
c = (Outlook.ContactItem)items;


Yes, this is a kind of restriction in version-neutral interops. Try c =
items.Item(i) as Outlook.ContactItem;

Also, you may want to read my post at the Add-in Express blog on how to
support several Outlook versions in a COM add-in using different interop
versions via early and late binding, see
http://www.add-in-express.com/creati...-late-binding/.

I am a rare visitor here. If you have any questions you can quickly reach me
on our forums, see http://www.add-in-express.com/forum/index.php.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com


"Marketware" wrote in message
news[i]
It appears as if I've got some weird "Using Statements" One that's just

Using Outlook;

and other,

Using Outlook1 = Microsoft.Office.Interop.Outlook;

And the original DLL on the above did not allow the "foreach". I found
another DLL which does allow it, and it seemed to work with both 2003 and
2007 so I think I may be OK with that. But do you know what the first one
is
pointing to? If I remove it I get a bunch of errors in the stuff you
wrote
for us.


"Marketware" wrote:

I am using the code you wrote to instaniate an Outlook object. I'm going
to
create a new project (separate out just this code) into a new project and
see
if I can see anything.

bob

"Ken Slovak - [MVP - Outlook]" wrote:

No idea, that is very bizarre.

I use that type of code all the time. It compiles perfectly here. In
fact, I
copied it from a working project I was just compiling.

I just tested and compiled again, and it compiled again with no errors.
There's some other problem that you have.

--
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
news Now I have a new compiler problem. On the line:

c = (Outlook.ContactItem)items;

I get the following:

Cannot apply indexing with [] to an expression of type
'Outlook.Items'

I've also tried to replace with () and I get another error about
trying to
use as a method.

Ideas??

.


  #9  
Old March 24th 10, 01:29 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#

In addition to what Andrei said if you updated your Add-In Express at any
point there might be references pointing to different paths or file names
that might need to be adjusted for a different version. For example in
loading a project created with an earlier version in the latest version of
Add-In Express I always have to adjust some references.

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


"Andrei Smolin [Add-in Express]"
wrote in message ...[i]
Hi Bob,

Using Outlook;


Add-in Express provides version-neutral interops. In your case the
interops are for Outlook 2000 and Office 2000 (common tools). The
namespaces contained in these interops are Outlook and Office,
respectively.

Using Outlook1 = Microsoft.Office.Interop.Outlook;


This statement refers to PIA for some Outlook version; it may be Outlook
2002, 2003 or 2007.

c = (Outlook.ContactItem)items;


Yes, this is a kind of restriction in version-neutral interops. Try c =
items.Item(i) as Outlook.ContactItem;

Also, you may want to read my post at the Add-in Express blog on how to
support several Outlook versions in a COM add-in using different interop
versions via early and late binding, see
http://www.add-in-express.com/creati...-late-binding/.

I am a rare visitor here. If you have any questions you can quickly reach
me on our forums, see http://www.add-in-express.com/forum/index.php.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook 2003 - CONTACTS - Unable to display folder folder is full Pearson385 Outlook - Using Contacts 7 September 5th 09 02:22 PM
Import contacts from Vista contact folder to outlook 2003 contacts bikeguy55 Outlook - Using Contacts 1 February 4th 09 05:51 PM
How to convert 2003 personal folder contacts to Outlook 2007 Carol Outlook - Using Contacts 2 October 25th 07 03:57 PM
Type Mismatch when iterating through eMail-folder MailItems in Outlook 2003 [email protected] Outlook and VBA 3 October 5th 07 02:45 PM
How to share outlook 2003 customize contacts or contacts subfolder without do it on public folder? Sue Mosher [MVP-Outlook] Outlook - Using Contacts 4 June 28th 07 10:45 PM


All times are GMT +1. The time now is 12:59 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.