473,499 Members | 1,568 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 19307
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\MSOUTL9.OLB) as
i dont have Outlook XP. At code level there should not be
any problem if u refer to the XP libraries(unless 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_PullOutlookContactsFromCSharp
{
/// <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.fnPrintOutlookContactsInfo();

}
}

class OutlookClient
{
public void fnPrintOutlookContactsInfo()
{

Outlook.Application
l_objOutlookApp =new Outlook.Application();
Outlook.MAPIFolder cContacts;
Outlook.Items l_objContactItems;
Outlook.ContactItem
l_objContactItem;
int l_intContactItemCtr ;

// Get NameSpace.
Outlook.NameSpace
l_objOutlookNamespace = 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_objOutlookNamespace.Logon
("SR", "", true, true);

// Get all the Contacts Folder
cContacts =
l_objOutlookNamespace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);

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

try
{
for
(l_intContactItemCtr=1;l_intContactItemCtr<=l_objC ontactIte
ms.Count;l_intContactItemCtr++)
{
l_objContactItem =
(Outlook.ContactItem)l_objContactItems.Item
(l_intContactItemCtr);
// Display some
common properties.
print("------------
-------------------------------------------------");
print("Information
for Contact #" + l_intContactItemCtr + " of Total Contacts
#" + l_objContactItems.Count);
print("------------
-------------------------------------------------");

print("Full
Name : " + l_objContactItem.FullName);
print("Title : "
+ l_objContactItem.Title);
print
("BirthDay : " + l_objContactItem.Birthday);
print
("CompanyName : " + l_objContactItem.CompanyName);
print
("Department : " + l_objContactItem.Department);
print("Body : " +
l_objContactItem.Body);
print("FileAs : "
+ l_objContactItem.FileAs);
print
("Email1Address : " + l_objContactItem.Email1Address);
print
("BusinessHomePage : " +
l_objContactItem.BusinessHomePage);
print
("MailingAddress : " + l_objContactItem.MailingAddress);
print
("BusinessAddress : " + l_objContactItem.BusinessAddress);
print
("OfficeLocation : " + l_objContactItem.OfficeLocation);
print
("Subject : " + l_objContactItem.Subject);
print
("JobTitle : " + l_objContactItem.JobTitle);
print("------------
-------------------------------------------------");
Console.WriteLine
("");
// 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_objContactItem.Display(true);
}

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

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

// Log off.

l_objOutlookNamespace.Logoff();

// Clean up.
l_objOutlookApp = null;
l_objOutlookNamespace =
null;
l_objContactItems = null;
l_objContactItem = null;

}
}
public void print(object o)
{
if(o != null && o.ToString
()!="")
{
Console.WriteLine
(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.WriteLine((l_objOutlookApp.GetNamespace
("mapi")).GetType());
// Prints "Outlook.NameSpaceClass"

So try out the following

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

//Outlook.NameSpace l_objOutlookNamespace =
l_objOutlookApp.GetNamespace("mapi");
Outlook.NameSpaceClass l_objOutlookNamespace =
(Outlook.NameSpaceClass) l_objOutlookApp.GetNamespace
("mapi");//watch out for line wraps.

2.
//Comment the original assignment statement
//Outlook.NameSpace l_objOutlookNamespace =
l_objOutlookApp.GetNamespace("mapi");

//Add the following modified line
Outlook.NameSpaceClass l_objOutlookNamespace =
(Outlook.NameSpaceClass) 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._Application 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.com>
To: <rs*****@hotmail.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.InvalidCastException' occurred inDummy_PullOutlookContactsFromCSharp.exe

Additional information: QueryInterface for interface Outlook._Applicationfailed.

It is right on this line:

Outlook.NameSpace l_objOutlookNamespace =
l_objOutlookApp.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\MSOUTL9.OLB) as i dont have Outlook XP. At code level there should not be any problem if u refer to the XP libraries(unless 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_PullOutlookContactsFromCSharp
{
/// <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.fnPrintOutlookContactsInfo();

}
}

class OutlookClient
{
public void fnPrintOutlookContactsInfo()
{

Outlook.Application
l_objOutlookApp =new Outlook.Application();
Outlook.MAPIFolder cContacts;
Outlook.Items l_objContactItems;
Outlook.ContactItem
l_objContactItem;
int l_intContactItemCtr ;

// Get NameSpace.
Outlook.NameSpace
l_objOutlookNamespace = 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_objOutlookNamespace.Logon
("SR", "", true, true);

// Get all the Contacts Folder
cContacts =
l_objOutlookNamespace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);

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

try
{
for
(l_intContactItemCtr=1;l_intContactItemCtr<=l_objC ontactIte ms.Count;l_intContactItemCtr++)
{
l_objContactItem =
(Outlook.ContactItem)l_objContactItems.Item
(l_intContactItemCtr);
// Display some
common properties.
print("------------
-------------------------------------------------");
print("Information
for Contact #" + l_intContactItemCtr + " of Total Contacts #" + l_objContactItems.Count);
print("------------
-------------------------------------------------");

print("Full
Name : " + l_objContactItem.FullName);
print("Title : "
+ l_objContactItem.Title);
print
("BirthDay : " + l_objContactItem.Birthday);
print
("CompanyName : " + l_objContactItem.CompanyName);
print
("Department : " + l_objContactItem.Department);
print("Body : " +
l_objContactItem.Body);
print("FileAs : "
+ l_objContactItem.FileAs);
print
("Email1Address : " + l_objContactItem.Email1Address);
print
("BusinessHomePage : " +
l_objContactItem.BusinessHomePage);
print
("MailingAddress : " + l_objContactItem.MailingAddress); print
("BusinessAddress : " + l_objContactItem.BusinessAddress); print
("OfficeLocation : " + l_objContactItem.OfficeLocation); print
("Subject : " + l_objContactItem.Subject);
print
("JobTitle : " + l_objContactItem.JobTitle);
print("------------
-------------------------------------------------");
Console.WriteLine
("");
// 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_objContactItem.Display(true);
}

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

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

// Log off.

l_objOutlookNamespace.Logoff();

// Clean up.
l_objOutlookApp = null;
l_objOutlookNamespace =
null;
l_objContactItems = null;
l_objContactItem = null;

}
}
public void print(object o)
{
if(o != null && o.ToString
()!="")
{
Console.WriteLine
(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
5704
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...
4
17451
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...
9
3062
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...
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...
1
4316
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...
1
4839
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...
11
7640
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...
6
2624
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...
3
3377
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...
0
7132
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,...
0
7009
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...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
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...
0
7390
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...
1
4919
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...
0
3103
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.