C# - Can't get list of outlook contacts on some machines  
Author Message
Clinton Chang





PostPosted: Visual C# General, C# - Can't get list of outlook contacts on some machines Top

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


Visual C#13  
 
 
Clinton Chang





PostPosted: Visual C# General, C# - Can't get list of outlook contacts on some machines Top

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.

 
 
ripern





PostPosted: Visual C# General, C# - Can't get list of outlook contacts on some machines Top

I have the same problem.

How to ignore them
When I cast a distribution list to Outlook.ContactItem it throws an exception. That happends here: foreach (Outlook.ContactItem contact in contactFolder.Items)

So instead I tried using GetFirst and GetNext but then I get an infinite loop which is described here: http://msdn2.microsoft.com/en-us/library/aa155748(office.10).aspx

How do I, in C#, write so GetNext doesn't pick up a new reference to the ContactFolder each time it is called Right now I'm doing:
// Get the next contact
contactItem = contactFolder.Items.GetNext();

where contactFolder is:
Outlook.MAPIFolder contactFolder = this.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

Thanks and happy new year! : )