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
(e-mail address removed) 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_objContactIte
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
.