473,769 Members | 3,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# outlook won't get list of contacts

2 New Member
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.26 25) 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.Ou tlook.SecurityM anager();
try
{
Outlook._Applic ation outlookObj = new Outlook.Applica tion();
Outlook.MAPIFol der fldContacts =
(Outlook.MAPIFo lder)outlookObj .Session.GetDef aultFolder(Outl ook.OlDefaultFo lders.olFolderC ontacts);
Outlook.Items searchFolder = fldContacts.Ite ms;
Outlook.Contact Item contactItem = (Outlook.Contac tItem)searchFol der.GetFirst();

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

SecurityManager .ConnectTo(outl ookObj);
SecurityManager .DisableOOMWarn ings = true;

while (contactItem != null)
{
dr = dt.NewRow();
dr[0] = contactItem.Fir stName;
dr[1] = contactItem.Las tName;
//etc etc etc
dt.Rows.Add(dr) ;
contactItem = (Outlook.Contac tItem)searchFol der.GetNext();
}

dv = new DataView(dt);
dv.Sort = "First Name ASC";
dgContacts.Data Source = dv;
}
catch (Exception ex)
{
Console.WriteLi ne(ex);
MessageBox.Show ("Contact List could not be retrieved from Outlook", "Contacts not retrieved",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
this.Close();
}
finally
{
SecurityManager .DisableOOMWarn ings = 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 4867
dcd
2 New Member
Hmmmm did a bit more digging it's failing on the cast:
contactItem = (Outlook.Contac tItem)searchFol der.GetNext();
in the while loop.

The error is:
System.InvalidC astException: Unable to cast COM object of type 'System.__ComOb ject' to interface type "Outlook.Contac tItem".

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
17481
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 address is selected, populate an Access field with the address. Is this remotely possible? Thanks, lq
5
3651
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 address, I want to open a new mail message to that person. I don't want to send the message, just open a new blank email so that I can make it easier to send messages. Any idea how I could find out more about how to do this ?
9
3092
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 oItems" loop. After about 250 contacts, the program gives an InvalidCastException Error on the for each loop. I notice that Outlook's memory keeps increasing (using the task manager) until it reaches around 20,000K. When I run the program a second...
16
4359
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 you get the security warning.
7
3628
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 get to item 250 i get a "system.invalid cast exception" and "specified cast is not valid". Once i get this error and try to go to the next record.. every record after gets the same error... It's like the com connection to outlook has been lost....
1
1803
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 contacts, but when PersonB & C go into the database with there login(i.e from there pc, the database shows up but the drodown data for Person'As contacts is not available In short how can other users have access to this data base
2
2766
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 run a macro which runs a select query which strips out and exports a list of their e-mail addresses ( separated by commas) into a text file. I then copy the CSV e-mail addresses and paste them into the BCC field of Outlook. I'd love to have a...
4
13939
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 SenderNameDialog. I need to support both versions. Any hints? TIA
11
7737
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 enabled me to the contacts at least list. I had to root around to discover CdoDefaultFolderContacts (though it was guessable; how could I enumerate win32com.client.constants?). I now want to work through the Contacts in Outlook patching in...
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.