473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# outlook won't get list of contacts

2
Hi all I'm using trying to get my app to read in all contacts in the contact folder of Outlook. I'm using the Outlook Security manager to stop the pop up warnings. Outlook version is 2002(10.2627.2625) I can create a contact in Outlook and I can use it to send an email. It's just the getting the contacts list that doesn't work. The thing is it also works on my dev machine but not on the users machine.

The code I'm using:
SecurityManager = new AddinExpress.Outlook.SecurityManager();
try
{
Outlook._Application outlookObj = new Outlook.Application();
Outlook.MAPIFolder fldContacts =
(Outlook.MAPIFolder)outlookObj.Session.GetDefaultF older(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items searchFolder = fldContacts.Items;
Outlook.ContactItem contactItem = (Outlook.ContactItem)searchFolder.GetFirst();

//Code to create a data table to put the contact info into.

SecurityManager.ConnectTo(outlookObj);
SecurityManager.DisableOOMWarnings = true;

while (contactItem != null)
{
dr = dt.NewRow();
dr[0] = contactItem.FirstName;
dr[1] = contactItem.LastName;
//etc etc etc
dt.Rows.Add(dr);
contactItem = (Outlook.ContactItem)searchFolder.GetNext();
}

dv = new DataView(dt);
dv.Sort = "First Name ASC";
dgContacts.DataSource = dv;
}
catch (Exception ex)
{
Console.WriteLine(ex);
MessageBox.Show("Contact List could not be retrieved from Outlook", "Contacts not retrieved",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
finally
{
SecurityManager.DisableOOMWarnings = false;
}


Can anyone see anything wrong here? I for the life of me can't figure out what is going on. I thought that the problem was with different versions of Outlook, but they are the same. Oh and I'm not using the enumeration for the Contacts Folder because Outlook 2002 doesn't support it. If I tried to use this code on Outlook 2003 would it work?
Nov 30 '06 #1
1 4803
dcd
2
Hmmmm did a bit more digging it's failing on the cast:
contactItem = (Outlook.ContactItem)searchFolder.GetNext();
in the while loop.

The error is:
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type "Outlook.ContactItem".

This is because he has a distribution list in his contacts folder and I have none. So I guess if anyone else is trying to do this same sort of operation watch out for distribution lists and ignore them if they come up.

Cheers
Nov 30 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: lauren quantrell | last post by:
Is there a way to open the MS Outlook address book using VBA and then be able to do something with the return value? I want users to click an icon to open the Outlook address book then when an...
5
by: TM | last post by:
I would like to write a little app in Visual Basic .net as an add-in to Outlook 2003 so that I can quickly get a list of contact names and email addresses, then when I click on a name or email...
9
by: George McCullen | last post by:
I have an Outlook 2003 using Exchange Server 2003 Public Contacts Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program that loops through all the contacts in a "for each oCt in...
16
by: ML | last post by:
Is there anyway to get around the security prompt from Outlook when using the object in a vb.net winforms app? I want to be able to load the contacts lists from Outlook but each time it access it...
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
1
by: tyronne | last post by:
I have created a data base that is linked to contacts in personA's outlook contacts, I need personB and PersonC to be able to also edit the data base which include a dropdown for personA's...
2
by: Tomdhu | last post by:
I've got a Contacts & Events database in Access 2K where contacts attend various events. At present, where I want to group e-mail all the attendees who are going to attend a particular event, I...
4
by: Alexander Szigetvary | last post by:
Hi NG! Is there a way to open the Outlook address book dialog (as in Outlook) using C#? I am using Outlook 2003 (MS Outlook 11.0 Object Library). In Outlook 2007 it can be done with...
11
by: Bill Davy | last post by:
I am trying to edit Contacts in Outlook. This is so I can transfer numbers from my address book which is an Excel spreadsheet to my mobile phone. I came across the following snippet of code which...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.