By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

C# outlook won't get list of contacts

P: 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
Share this Question
Share on Google+
1 Reply


P: 2
dcd
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

Post your reply

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