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

  • Thread starter Thread starter Marketware
  • Start date Start date
Status
Not open for further replies.
M

Marketware

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

"Marketware" <Marketware> wrote in message

news:57EA1D70-7651-4F3D-9F2B-CB1542CAFF64@microsoft.com...
> 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.GetDefaultFolder(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
 
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??
wrote:


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

> >

>

> "Marketware" <Marketware> wrote in message
> news:57EA1D70-7651-4F3D-9F2B-CB1542CAFF64@microsoft.com...
> >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.GetDefaultFolder(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


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

"Marketware" <Marketware> wrote in message

news:D06519EB-B9A1-45F0-9644-DC177DB17ECA@microsoft.com...
> 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??
 
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
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.

> >

>

> "Marketware" <Marketware> wrote in message
> news:D06519EB-B9A1-45F0-9644-DC177DB17ECA@microsoft.com...
> > 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??


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

"Marketware" <Marketware> wrote in message

news:4A58A327-D787-4627-ACDC-1EAB13B08370@microsoft.com...
> 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
 
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

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

> >

> > "Marketware" <Marketware> wrote in message
> > news:D06519EB-B9A1-45F0-9644-DC177DB17ECA@microsoft.com...
> > > 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??

> > .
> >
 
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/creating-addins-blog/2010/03/16/interop-assemblies-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" <Marketware> wrote in message

news:D0E28A3F-CE5F-4A58-9F85-18972BA29090@microsoft.com...
> 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
>

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

> > >

> >>>>>>> > "Marketware" <Marketware> wrote in message
> > > news:D06519EB-B9A1-45F0-9644-DC177DB17ECA@microsoft.com...
> > > > 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??
> >> > .
> > >
 
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.

"Andrei Smolin [Add-in Express]" <andrei_ddoott_smolin@add-in-express.com
wrote in message news:e9k9AF0yKHA.2644@TK2MSFTNGP04.phx.gbl...
> 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/creating-addins-blog/2010/03/16/interop-assemblies-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
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
F Outlook's contacts Using Outlook 1
kburrows Outlook Automatically Merging Contacts Using Outlook 2
M Retaining old contacts and calendar in new account setup Using Outlook 6
efire9207 VBA Outlook Contacts Outlook VBA and Custom Forms 6
C Change Photo Size of Selected Contacts Outlook VBA and Custom Forms 1
N Best way to sync calendar and contacts between Outlook 365 and Outlook on iPhone Using Outlook 4
O Any 3rd party tool that sync (mirror) from Outlook Contacts to Google Contacts? Using Outlook 4
G Apply Custom Contacts form to all existing Contacts Outlook VBA and Custom Forms 1
G Add Map It button to Custom Contacts Form in Outlook Outlook VBA and Custom Forms 1
B Sync Outlook Public Folders to Contacts Using Outlook 2
Kika Melo How to mark as Junk any message not from Contacts (in Outlook.com) Using Outlook 3
G Outlook 365 My iCloud Outlook doesn’t work after reinstalling Microsoft365 on Windows 10 PC – now I get error message on contacts and calendar Using Outlook 1
T How to Export & Import GMAIL Contacts into Outlook 2021 ? Using Outlook 4
L Applying new form to existing contacts -- MessageClass Outlook VBA and Custom Forms 3
Bardiferous Weird Contacts Behavior - Can't find any in any address book, but they're in there Using Outlook 3
S client lost 3K contacts when office 365 for mac account deleted from company exchange server Using Outlook 5
O How to sync (one way) contacts between two Outlook (exchange) accounts? Using Outlook 0
P How to clear out all contacts in iCloud, so I can use iCloud-based sync program Using Outlook 1
R How to prevent corrupted Notes format in Calendar and Contacts Using Outlook 0
K Outlook 365 After migrating to Outlook 365, some contacts display in emails with prefixes Using Outlook 0
kburrows Outlook or Phone Combining Contacts? Using Outlook 0
Witzker Add a text line at the end of the note field in all selected Contacts Outlook VBA and Custom Forms 7
G Add contacts birthday to calendar Using Outlook 4
R How to force Outlook to use plain text in notes for Contacts? Using Outlook 1
S Cannot print Contacts Using Outlook 7
V Backup Calendar, Contacts, Tasks in an POP3 data file Using Outlook 3
J Outlook 365 Outlook 2016/365 Contacts Lose Info when Favorited to the To-Do Bar Using Outlook 2
D Advanced e-Mail search on from/to contact group only searches for first 20 contacts in group Using Outlook 0
P Outlook calendar and contacts sync problem-outlook disconnects Using Outlook.com accounts in Outlook 2
K Font Sizing in Custom Form Regions for Contacts Outlook VBA and Custom Forms 1
O Outlook - hidden contacts? Using Outlook 2
H Synchronize contacts and calendars across multiple devices Using Outlook 0
S Outlook 2016 Change how Outlook shows me contacts in emails Using Outlook 0
L Favorites for People/Contacts? Using Outlook 1
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
Horsepower Contacts added from iPhone don't show up in Outlook Using Outlook 2
B oContacts.Items.Restrict Misses Some Contacts Outlook VBA and Custom Forms 3
F Moving Contacts to New Profile Using Outlook 0
V Contacts in O365 have changed?? Using Outlook 20
T Outlook 2010 BCM Migration to Standard Contacts BCM (Business Contact Manager) 0
D Outlook Contacts Notes Field Photos to Smartphone Using Outlook 0
G Add to Outlook Contacts - Point to non-default contacts folder Using Outlook 0
D Is a sub folder under contacts necessary to be able to name an Address Book? Using Outlook 1
L Outlook 2016 Contacts missing in Outlook 365 Using Outlook 1
L icloud contacts sync Using Outlook 4
Fozzie Bear Outlook 2016 Creating a shared local Contacts folder Using Outlook 2
M VbScript for Command Button on Contacts Custom Form Using Outlook 1
J Outlook 2013 Change color of text in data fields of contacts in Outlook 2013? Using Outlook 10
M Changing the preferred order for "Put this entry in" list for adding new contacts to the Address Book Using Outlook 1
GregS Exporting contacts Using Outlook 1

Similar threads

Back
Top