473,597 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting Outlook Contacts with C#

Can anyone provide a small snippet in C# that pulls out the Contacts in
Outlook XP.

I've seen a couple of examples in C++ and VB in previous newsgroup posts,
but either the originals didn't work or my conversion skills are weak. And
if I have to use tlbimp.exe what is the right file to use.

Thanks for the help.
--
Fritz
Jul 19 '05 #1
2 19322
SR
Hi Fritz

thought ill give it a go and here u are.. It will loop
thru ur outlook app and print the info of each contact
item.

Pls note the following

a) the code is in C# as u require it

b) i set reference in vs.net to the Outlook 2000 library
(C:\Program Files\Microsoft Office\Office\M SOUTL9.OLB) as
i dont have Outlook XP. At code level there should not be
any problem if u refer to the XP libraries(unles s MS has
changed some basic interfaces. If u do not use VS.Net and
want to do a tlb, check for a similar file in ur office
folder.
c) Watch out for linewraps. if the code gets wrapped
(warped :) ) in this post, then mail me at
rs*****@hotmail .com and would be happy to send u the code
in a VS.Net solution format(zipped)

Here is the code
using System;
using Outlook;

namespace Dummy_PullOutlo okContactsFromC Sharp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start
application here
//
OutlookClient
l_objOutClient= new OutlookClient() ;

l_objOutClient. fnPrintOutlookC ontactsInfo();

}
}

class OutlookClient
{
public void fnPrintOutlookC ontactsInfo()
{

Outlook.Applica tion
l_objOutlookApp =new Outlook.Applica tion();
Outlook.MAPIFol der cContacts;
Outlook.Items l_objContactIte ms;
Outlook.Contact Item
l_objContactIte m;
int l_intContactIte mCtr ;

// Get NameSpace.
Outlook.NameSpa ce
l_objOutlookNam espace = l_objOutlookApp .GetNamespace
("mapi");

// Logon. If an outlook app is
already open, then it will reuse that session. Else
// it will perform a fresh
logon. Note that if u have psts and passwords for the
// same, u need to enter the
passwords in the dialogbox when they are shown
l_objOutlookNam espace.Logon
("SR", "", true, true);

// Get all the Contacts Folder
cContacts =
l_objOutlookNam espace.GetDefau ltFolder
(Outlook.OlDefa ultFolders.olFo lderContacts);

//get all the COntacts from the
Contacts Folder
l_objContactIte ms =
cContacts.Items ;

try
{
for
(l_intContactIt emCtr=1;l_intCo ntactItemCtr<=l _objContactIte
ms.Count;l_intC ontactItemCtr++ )
{
l_objContactIte m =
(Outlook.Contac tItem)l_objCont actItems.Item
(l_intContactIt emCtr);
// Display some
common properties.
print("------------
-------------------------------------------------");
print("Informat ion
for Contact #" + l_intContactIte mCtr + " of Total Contacts
#" + l_objContactIte ms.Count);
print("------------
-------------------------------------------------");

print("Full
Name : " + l_objContactIte m.FullName);
print("Title : "
+ l_objContactIte m.Title);
print
("BirthDay : " + l_objContactIte m.Birthday);
print
("CompanyNam e : " + l_objContactIte m.CompanyName);
print
("Department : " + l_objContactIte m.Department);
print("Body : " +
l_objContactIte m.Body);
print("FileAs : "
+ l_objContactIte m.FileAs);
print
("Email1Addr ess : " + l_objContactIte m.Email1Address );
print
("BusinessHomeP age : " +
l_objContactIte m.BusinessHomeP age);
print
("MailingAddres s : " + l_objContactIte m.MailingAddres s);
print
("BusinessAddre ss : " + l_objContactIte m.BusinessAddre ss);
print
("OfficeLocatio n : " + l_objContactIte m.OfficeLocatio n);
print
("Subject : " + l_objContactIte m.Subject);
print
("JobTitle : " + l_objContactIte m.JobTitle);
print("------------
-------------------------------------------------");
Console.WriteLi ne
("");
// If you want the
contact to be displayed, then uncomment the line below
// After each item
info is printed, the contact window will be shown and
// only on closing
that window, will the next contact be shown
//l_objContactIte m.Display(true) ;
}

}
catch(System.Ex ception
p_objException)
{
print("Error : " +
p_objException. Message);
}
finally
{

Console.Write(" ");
Console.Write(" Press Enter
to quit application");
Console.ReadLin e ();
Console.Write(" ");
Console.Write(" Logging off
and Closing Outlook Application. This will take a
moment.Pls wait...");

// Log off.

l_objOutlookNam espace.Logoff() ;

// Clean up.
l_objOutlookApp = null;
l_objOutlookNam espace =
null;
l_objContactIte ms = null;
l_objContactIte m = null;

}
}
public void print(object o)
{
if(o != null && o.ToString
()!="")
{
Console.WriteLi ne
(o);
}
}

}
}

hth

regards,

sr
-----Original Message-----
Can anyone provide a small snippet in C# that pulls out the Contacts inOutlook XP.

I've seen a couple of examples in C++ and VB in previous newsgroup posts,but either the originals didn't work or my conversion skills are weak. Andif I have to use tlbimp.exe what is the right file to use.

Thanks for the help.
--
Fritz
.

Jul 19 '05 #2
SR

Hi

with referene to your mail below :
Must be an interop issue, though it works on my machine.
When i print the type of the return value of the
Getnamespace method it is as follows

Console.WriteLi ne((l_objOutloo kApp.GetNamespa ce
("mapi")).GetTy pe());
// Prints "Outlook.NameSp aceClass"

So try out the following

1.
//Comment the original assignment statement
//Add the following modified line

//Outlook.NameSpa ce l_objOutlookNam espace =
l_objOutlookApp .GetNamespace(" mapi");
Outlook.NameSpa ceClass l_objOutlookNam espace =
(Outlook.NameSp aceClass) l_objOutlookApp .GetNamespace
("mapi");//watch out for line wraps.

2.
//Comment the original assignment statement
//Outlook.NameSpa ce l_objOutlookNam espace =
l_objOutlookApp .GetNamespace(" mapi");

//Add the following modified line
Outlook.NameSpa ceClass l_objOutlookNam espace =
(Outlook.NameSp aceClass) l_objOutlookApp .GetNamespace
("mapi"); //watch out for line wraps.
let me know if this also gives problems...Also once the
l_objOutlookApp is initialized, check the value of the obj
var in the watch window.

The funny part is why is the queryinterface for the
outlook._Applic ation failing??? Im able to see this in
the ILDASM of my interop assembly. If possible mail me the
interop assembly of the Outlook that is created(zipped) so
that i can use ILDASM to check it out

would appreciate any comments from others also

regards,

sr


regards,

sr
From: "Fritz Switzer" <fr***********@ abletfactory.co m>
To: <rs*****@hotmai l.com>
Subject: Outlook Contacts code
Date: Wed, 13 Aug 2003 13:45:52 -0500

SR,

Thanks for the code sample. It looks very close and I really appreciate theeffort.

The word wrap was a problem but I think I got everything cleared up. But
I am getting an exception on the NameSpace call: Here is the error.
An unhandled exception of type 'System.Invalid CastException' occurred inDummy_PullOutl ookContactsFrom CSharp.exe

Additional information: QueryInterface for interface Outlook._Applic ationfailed.

It is right on this line:

Outlook.NameSpa ce l_objOutlookNam espace =
l_objOutlookAp p.GetNamespace( "mapi");

I am running Outlook 2000.

I set my reference to MSOUTL9.OLB and using Outlook;

Any thoughts?

TIA

Fritz

<< winmail.dat >> -----Original Message-----
SR,

Great reply.

Thanks, that looks like exactly what I need. I'll give it a try.
Fritz

--
Fritz Switzer
View my Ink Blog at:
www.abletFactory.com

SR wrote:
Hi Fritz

thought ill give it a go and here u are.. It will loop
thru ur outlook app and print the info of each contact
item.

Pls note the following

a) the code is in C# as u require it

b) i set reference in vs.net to the Outlook 2000 library
(C:\Program Files\Microsoft Office\Office\M SOUTL9.OLB) as i dont have Outlook XP. At code level there should not be any problem if u refer to the XP libraries(unles s MS has
changed some basic interfaces. If u do not use VS.Net and want to do a tlb, check for a similar file in ur office
folder.
c) Watch out for linewraps. if the code gets wrapped
(warped :) ) in this post, then mail me at
rs*****@hotmail .com and would be happy to send u the code in a VS.Net solution format(zipped)

Here is the code
using System;
using Outlook;

namespace Dummy_PullOutlo okContactsFromC Sharp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start
application here
//
OutlookClient
l_objOutClient= new OutlookClient() ;

l_objOutClient. fnPrintOutlookC ontactsInfo();

}
}

class OutlookClient
{
public void fnPrintOutlookC ontactsInfo()
{

Outlook.Applica tion
l_objOutlookApp =new Outlook.Applica tion();
Outlook.MAPIFol der cContacts;
Outlook.Items l_objContactIte ms;
Outlook.Contact Item
l_objContactIte m;
int l_intContactIte mCtr ;

// Get NameSpace.
Outlook.NameSpa ce
l_objOutlookNam espace = l_objOutlookApp .GetNamespace
("mapi");

// Logon. If an outlook app is
already open, then it will reuse that session. Else
// it will perform a fresh
logon. Note that if u have psts and passwords for the
// same, u need to enter the
passwords in the dialogbox when they are shown
l_objOutlookNam espace.Logon
("SR", "", true, true);

// Get all the Contacts Folder
cContacts =
l_objOutlookNam espace.GetDefau ltFolder
(Outlook.OlDefa ultFolders.olFo lderContacts);

//get all the COntacts from the
Contacts Folder
l_objContactIte ms =
cContacts.Items ;

try
{
for
(l_intContactIt emCtr=1;l_intCo ntactItemCtr<=l _objContactIte ms.Count;l_intC ontactItemCtr++ )
{
l_objContactIte m =
(Outlook.Contac tItem)l_objCont actItems.Item
(l_intContactIt emCtr);
// Display some
common properties.
print("------------
-------------------------------------------------");
print("Informat ion
for Contact #" + l_intContactIte mCtr + " of Total Contacts #" + l_objContactIte ms.Count);
print("------------
-------------------------------------------------");

print("Full
Name : " + l_objContactIte m.FullName);
print("Title : "
+ l_objContactIte m.Title);
print
("BirthDay : " + l_objContactIte m.Birthday);
print
("CompanyNam e : " + l_objContactIte m.CompanyName);
print
("Department : " + l_objContactIte m.Department);
print("Body : " +
l_objContactIte m.Body);
print("FileAs : "
+ l_objContactIte m.FileAs);
print
("Email1Addr ess : " + l_objContactIte m.Email1Address );
print
("BusinessHomeP age : " +
l_objContactIte m.BusinessHomeP age);
print
("MailingAddres s : " + l_objContactIte m.MailingAddres s); print
("BusinessAddre ss : " + l_objContactIte m.BusinessAddre ss); print
("OfficeLocatio n : " + l_objContactIte m.OfficeLocatio n); print
("Subject : " + l_objContactIte m.Subject);
print
("JobTitle : " + l_objContactIte m.JobTitle);
print("------------
-------------------------------------------------");
Console.WriteLi ne
("");
// If you want the
contact to be displayed, then uncomment the line below
// After each item
info is printed, the contact window will be shown and
// only on closing
that window, will the next contact be shown
//l_objContactIte m.Display(true) ;
}

}
catch(System.Ex ception
p_objException)
{
print("Error : " +
p_objException. Message);
}
finally
{

Console.Write(" ");
Console.Write(" Press Enter
to quit application");
Console.ReadLin e ();
Console.Write(" ");
Console.Write(" Logging off
and Closing Outlook Application. This will take a
moment.Pls wait...");

// Log off.

l_objOutlookNam espace.Logoff() ;

// Clean up.
l_objOutlookApp = null;
l_objOutlookNam espace =
null;
l_objContactIte ms = null;
l_objContactIte m = null;

}
}
public void print(object o)
{
if(o != null && o.ToString
()!="")
{
Console.WriteLi ne
(o);
}
}

}
}

hth

regards,

sr
-----Original Message-----
Can anyone provide a small snippet in C# that pulls out the Contacts in Outlook XP.

I've seen a couple of examples in C++ and VB in previous newsgroup posts, but either the originals didn't work or my conversion skills are weak. And if I have to use tlbimp.exe what is the right file to use.

Thanks for the help.
--
Fritz
.

.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5720
by: Kurt | last post by:
Hi We are developing an off-the-shelf software suite for a certain business sector. Most of the program is simply a GUI on top of some .mdb files. Its a .net application written in c# One major requirement is "Outlook Integration". The Outlook contact folders are to be used for contact management I want to know what is the standard way to do this these days. Most of the information in MSDN seems rather old I first used the Outlook...
4
17458
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
9
3076
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...
2
399
by: Fritz Switzer | last post by:
Can anyone provide a small snippet in C# that pulls out the Contacts in Outlook XP. I've seen a couple of examples in C++ and VB in previous newsgroup posts, but either the originals didn't work or my conversion skills are weak. And if I have to use tlbimp.exe what is the right file to use. Thanks for the help. -- Fritz
1
4327
by: charliej2001 | last post by:
Hi all My access database has import/export capabiltiy of contact details between outlook. The database is getting big now (1000+ contacts) and so are the outlook address books that have the contacts info in them. When I export contacts from access to outlook, it takes a long time because there are so many contacts in the Outlook address book, and it is checking all of these against the contacts coming in, so as to avoid duplicates....
1
4856
by: dcd | last post by:
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 =...
11
7690
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...
6
2636
by: Kevin | last post by:
In my business, I use Quickbooks extensively for billing and accounting and a custom built Access DB for project management. I recently began using MS Outlooks contacts quite extensively as well (recently bought a PDA to bring to job sites!). I have been able to get things set up so that I can synchronize my Quickbooks customer and vendor lists with the Outlook contacts, and thus with my PDA and this has been immensely helpful. My DB has...
3
3384
by: Volkan Senguel | last post by:
Hi Is there a easy way to get the contacts (names and phonenumbers) from outlook without the message that someone is accessing outlook and how long the access can take? i have not found any working solution for that :( thanks for any help Volkan
0
7981
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...
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8046
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,...
1
5847
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
5437
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
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2410
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
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
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.