More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 21-Jan-2017 10:07

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Outlook and .NET Printer Friendly Version
Title: Show AddressBook from CDO with C#
Description: opens the AddressBook from C# with CDO Library
Date: 22-Feb-2005  01:37
Code level: intermediate
Code area: Outlook and .NET
Posted by: Helmut Obertanner
This message is displayed as VB.NET
 using CDO = MAPI;		

                                          /// <summary>
		/// Shows a Dialog for selecting Recipients from AddressBook
		/// and returns the Selected Recipients
		/// </summary>
		/// <returns></returns>
		public string SelectRecipients(string CurrentSelectedRecipients)
		{
			string SelectedRecipients = CurrentSelectedRecipients; 
			try
			{
				// Show the Adressbook with CDO
				
				CDO.Recipients myRecipients = null; 
			
				object myMissing = Missing.Value ;

				CDO.SessionClass mySession = new CDO.SessionClass ();
				// mySession.Logon (myMissing,myMissing,false,false,myMissing,myMissing,myMissing);
				
				mySession.MAPIOBJECT = myOutlookApplication.Session.MAPIOBJECT;  

				System.Type myType = mySession.GetType ();

				object[] myArgs = new object []{myMissing,"Empfänger Auswählen...",myMissing,myMissing,1,"An"};
				myRecipients =  (CDO.Recipients) myType.InvokeMember ("AddressBook",BindingFlags.InvokeMethod ,null,mySession,myArgs);

				for(int i = 1; i <= (int)myRecipients.Count; i++ )
				{
					object myItem =  myRecipients.get_Item (i);
					System.Type myRecipientType = myItem.GetType ();
					
					string myEntryID = (string) myRecipientType.InvokeMember ("ID", BindingFlags.GetProperty ,null,myItem,null); 

					Ol.Recipient myRecpt = (Ol.Recipient ) myOutlookApplication.Session.GetRecipientFromID (myEntryID); 

					if (myRecpt != null)
					{
						SelectedRecipients += myRecpt.Name + "; ";
					}
				}
				myRecipients = null;

				// myRecipients = (Ol.Recipients) mySession.AddressBook(myRecipients, myMissing ,myMissing,myMissing, myMissing ,myMissing ,myMissing, myMissing,myMissing); 
                mySession.Logoff ();
				mySession = null;

				Debug.WriteLine (SelectedRecipients);
					
				return SelectedRecipients;
			}
			catch(System.Exception ex)
			{

			}
		}
All 52comments
Page [ 1 2 3 4 5 6 Next >>  
  11-Mar-2005  08:16   
Hi. I'm a beginner. My question is, How do I implement the code into a C# project?
  11-Mar-2005  08:24   
Hello Jon,

this Code is used in an Outlook AddIn, but can adopted to use from an Windows Application.

What type of Application do you want to programm ?
An extension for Outlook, or an external Application ?

Greets, Helmut Obertanner
 
  31-Mar-2005  09:11   
Hello Helmut ,
I am also a beginner. I would like to use your code in a command button - Using a form based on an outlook 2000 ipm.note or ipm.post built-in form. All I want if for the users to click the button - or click a dropdown - select the individual from the Xchange 2000 Server global address book (This is an internal form) and once select for that information to reflect in a field or show and save in an organizational forms library in the server. When the manager opens the message for all field - including the Global Address List selection to show

Any help to implement this code would be appreciated
  31-Mar-2005  10:33   
Hello Helmut ,
I am also a beginner. I would like to use your code in a command button - Using a form based on an outlook 2000 ipm.note or ipm.post built-in form. All I want if for the users to click the button - or click a dropdown - select the individual from the Xchange 2000 Server global address book (This is an internal form) and once select for that information to reflect in a field or show and save in an organizational forms library in the server. When the manager opens the message for all field - including the Global Address List selection to show

Any help to implement this code would be appreciated
  31-Mar-2005  10:57   
Hello Edgar,
Outlook2000:
is realy not the best plattform for an .Net AddIn.
O.K. make some things clear:

O.K. you want a custom form with an additional Button where the Addressbook is opened and you can select receipients from Global AddressBook.

This Post / Mail should be saved in a ?!? Public Folder.
O.K. since you are a Beginner i recommend the following.
1. Buy Sues Book. ;-)
2. Use a script in your Form with VBA Code and not an AddIn, either a .Net AddIn.
Design that form with Outlook2000 and publish that in the Public Folder.
Here on this site are all relevant Informations on designing, publishing custom forms and a lot of VBA samples.
I'm sure there is a sample showing addressbook from VBA also, if not i will find it for you.
A quick google search found this article, as usual from Sue Mosher.

http://www.windowsitpro.com/MicrosoftExchangeOutlook/Article/ArticleID/15547/15547.html

Greets, Helmut Obertanner
[DATALOG Software AG]

 
  05-Jan-2006  03:06   
Hi Helmut!
I tried to insert your code in my application but the problem is that I don't know who MAPI is. I suppose CDO is used here as an alias...Could anyone tell me what MAPI is ?
  05-Jan-2006  05:13   
Hello Anca,

you must add a reference to COM Library CDO 1.21 to your project.
MAPI is the Namespace of the CDO Library.
I just say using CDO = MAPI.

Hope this helps,
greets, Helmut
 
  12-Jan-2006  18:21   
Hi Helmut,

Iam trying to implement the code given in this article. My requirement is to provide search facility of email addresses to the targeted users in our project. So i have given a try after going thru this article. But iam struck up with the line of code:

mySession.MAPIOBJECT = myOutlookApplication.Session.MAPIOBJECT;

the C# compiler is giving an error, "The type or namespace name 'myOutlookApplication' could not be found (are you missing a using directive or an assembly reference?)".

what is "myOutlookApplication" variable here. it seems an user-defined variable of type ??

could you please clear my query so that i can proceede with my R&D work to meet the requiremet.

Thanks in advance.
  13-Jan-2006  02:00   
Hello Naga,

this is a sample is written to be used in Outlook addins where you can say access to cdo is safe and don't show a security dialog.

the myOutlookApplication is a variable holding the Outlook.Application reference.
 From outside an AddIn you can try it like this:
Outlook.Application myOutlookApplication = new Outlook.Application();

For AddIn:

Maybe have a look at this sample:
http://www.outlookcode.com/codedetail.aspx?id=797

Greets, Helmut Obertanner
[http://www.x4u.de]
  16-Jan-2006  14:57   
Hi Helmut,

Thanks for clearing my query and informaton given. Iam new to outlook automation thru ASP.NET Application(C#) and facing some issues in accessing outlook mails, forms(custom) in the project work. Iam still not clear of the correct approach to access Exchange Server thru C#.NET code. I jst understood that we can use either MAPI or CDO to access Exchange Server(Outlook) contents thru .NET apps. Is it right??? Which is the correct way to access Exchange Server and extract Outlook mails & custom forms thru an ASP.NET application(C# language in code-behind). I have a kind of requriement in the current project where i gotta access Outlook mails & custom defined Forms in the application. So i would like to know which is the correct method(MAPI/CDO/??) to access the Exchange Server(Outlook content) thru the ASP.NET application.

Hope to hear the clear explanation from you. Thank you very much in advance. Also please suggest me any book(s) that can discuss about access outlook content(especially mails & forms) thru C#.NET applications.

Thanks again,

~Naga
Page [ 1 2 3 4 5 6 Next >>