Tuesday, May 26, 2009

Read contacts from Outlook using javascript


Following Javascript uses MAPI component and reads the contact details stored in Outlook. This works only with Outlook, not with Outlook express. It can also read the GAB if outlook is connected(following code does not do that, it prints only the addresses stored. You can remove the check for 'Contacts' to do so..).


 
<html><head>
<script language="javascript">
<!---
function readOutlookContacts()
{
obj = new ActiveXObject("Outlook.Application");
mapi= obj.GetNamespace("MAPI");
if( mapi )
{
addlst = mapi.AddressLists;
if( addlst )
{
if( addlst.count > 0 )
{
for(i = 1; i <= addlst.Count;i )
{
al = addlst.Item(i);
addentrs = al.AddressEntries;
if(al.name=="Contacts") //no need to read GA Book or other folders
{
for( tmpi = 1; tmpi <= addentrs.Count; tmpi )
{
aentry = addentrs.Item(tmpi);
email =aentry.Address;
document.write(":" +email + <br>);
}}}}
}}
}
-->
</script>
</head>
<body onLoad=javascript:readOutlookContacts()>
</body>
</html>


But take care of IE security settings for activex while running this.

No comments: