Importing contacts from Outlook

Environment: VC6, Outlook 2000/XP.

Outlook has become a de facto standard in Personal Information Management software. Almost everyone uses this software for managing their needs. There arises the need for programmatically manipulating information stored in Outlook. Microsoft has provided the Outlook Object Model for this very same purpose. A closer look at the samples on MSDN reveals that almost all samples are in Visual Basic. What should the (not so poor ;-)) C++ programmer do about this? Because the Outlook Object Model is a collection of COM interfaces, any COM-compliant language can useit. This sample can import contacts from any contacts folder in Outlook.

To use Office/Outlook Objects in C++, following files needs to be imported...

For Office XP:

#import "E:\Program Files\Common Files\Microsoft Shared\Office10\
            mso.dll" named_guids
#import "E:\Microsoft Office\Office10\MSOUTL.OLB" \
            no_namespace exclude("_IRecipientControl",
            "_DRecipientControl")

For Office 2000:

#import "I:\My Programs\Office2K\Office\mso9.dll" named_guids

#import "I:\My Programs\Office2K\Office\MSOUTL9.OLB" \
            no_namespace exclude("_IRecipientControl",
            "_DRecipientControl")

This sample imports Contacts information but a slight modification will enable this to import any other information from Outlook as well. This includes Appointment Items, Email Messages, Notes, Tasks, and more.

For example, to import Appointment Items from the Calendar folder, one just needs to make an object of the _AppointmentItemPtr smart pointer class instead of _ContactItemPtr.

Code to import Contacts...

_ApplicationPtr pApp;
_ItemsPtr pItems;
MAPIFolderPtr pFolder;
_ContactItemPtr pContact;

HRESULT hr;

try
{

  hr=pApp.CreateInstance(__uuidof(Application));
  if (FAILED(hr))
  {
    MessageBox("Unable to instantiate Outlook.",
               "Outlook Error",MB_OK);
    return;
  }

  if (m_Option.GetCheck()) //default outlook contacts folder
  {
    pFolder=pApp->GetNamespace(_bstr_t("MAPI"))->
                    GetDefaultFolder(olFolderContacts);
    if (pFolder==NULL)
    {
      MessageBox("Could not find default contacts folder.",
                 "Outlook Error");
      return;
    }
    
  }
  else    //display folder selection window
  {
    pFolder=pApp->GetNamespace(_bstr_t("MAPI"))->PickFolder();
    if (pFolder==NULL)
      return;

    if (pFolder->GetDefaultItemType()!=olContactItem)
    {
      MessageBox("Select folder is not a Contact folder.",
                 "Outlook Contacts");
      return;
    }
  }

  pItems=pFolder->GetItems();
  if (pItems==NULL)
  {
    MessageBox("Unabel to get Contact Items.",
               "Outlook Error");
    return;
  }

  pContact=pItems->GetFirst();


  m_ContactList.ResetContent();

  while(1)
  {
    if (pContact==NULL)
      break;
    CString strTemp;
    strTemp=(char *)pContact->GetFullName();
    strTemp=strTemp + "<";
    strTemp=strTemp + (char *)pContact->GetEmail1Address();
    strTemp=strTemp + ">";
    m_ContactList.AddString(strTemp);

    pContact=pItems->GetNext();
  }

}
catch(_com_error &e)
{
  MessageBox((char *)e.Description());
}

Downloads

Download demo project - 6.49 Kb
Download source - 375 Kb


Comments

  • Get rid of the message box when running the program

    Posted by itaihor on 10/11/2005 02:34am

    When running the program, the next message box appears: "A program is trying to access e-mail addresses...." How can I get rid of this message?

    Reply
  • writing the same app for a console

    Posted by Andy Sponik on 04/22/2004 05:39am

    Hi, I'm trying to write the same app but for a console application. I'm not really familiar with c++ so I'm not sure what to do. Could someone tell me which changes I have to make to make this work (I'm trying to do this so later on I can export the contacts to a java app). thx, Andy

    Reply
  • Retrieving Calendar Info

    Posted by Legacy on 09/11/2003 12:00am

    Originally posted by: Chris Bartle

    I had been looking for a way to extract calendar data from Outlook 2000 and this code works well for that too. Just replace _ContactItemPtr with _AppointmentItemPtr and use GetDefaultFolder(olFolderCalendar). I've been having a hard time finding any good documentation from Microsoft on using these classes in VC, so I've been stuck decoding the header files. Still, this code provides a great launching point for accessing more than just Outlook contacts.

    Reply
  • How to import and export vcard files from Outlook in vc application?

    Posted by Legacy on 08/12/2003 12:00am

    Originally posted by: Dennis liao

    Dear all:
    Do anybody knows How to import and export vcard files from Outlook in vc application?
    Hoping for a reply,
    Thanks and Regards,
    Dennis liao

    Reply
  • Creating Email Account..

    Posted by Legacy on 03/03/2003 12:00am

    Originally posted by: Paras Shah

    Hi there,

    Is there any possibility to create/modify the email account in Outlook 2002(office xp) programmatically using vc++?

    Paras Shah

    Reply
  • Doesn't return email address

    Posted by Legacy on 02/10/2003 12:00am

    Originally posted by: majk

    Hello,

    it's elegant concept. The problem I have:
    'GetEmail1Address' returns just string like here: '/o=XYZ/ou=ABC012/cn=Recipients/cn=Firstname Lastname'
    'GetEmail2Address' and 'GetEmail3Address' return empty string.
    Any idea?

    M.

    Reply
  • Dynamic Import files

    Posted by Legacy on 12/25/2002 12:00am

    Originally posted by: kc

    How to dynamic import mso9.dll and MSOUTL9.OLB in my program? I got the file path from the Registry but I couldn't do it dynamically in my program.

    #import "C:\\Program Files\\Microsoft Office\\Office\\mso9.dll" named_guids

    #import "C:\\Program Files\\Microsoft Office\\Office\\MSOUTL9.OLB" \
    no_namespace exclude("_IRecipientControl", "_DRecipientControl")

    Reply
  • how to make "Select folder" open in a center?

    Posted by Legacy on 09/25/2002 12:00am

    Originally posted by: dima

    namespace::PickFolder() method displays outlook's "Select Folder" window in a corner. Does anyone know how to force the window to be opened in a center?

    Reply
  • What namespaces exist

    Posted by Legacy on 08/29/2002 12:00am

    Originally posted by: Peter Nelson

    Is there a way to enumerate the namespaces that can be retrieved? You get the 'MAPI' namespace each time - but that returns no contacts to me - even though when I open up Outlook I've got dozens. I'm assuming that my user is not set up as an exchange user, and so has no Mapi entries... but there must still be a way.

    -Peter

    Reply
  • Great Code

    Posted by Legacy on 07/30/2002 12:00am

    Originally posted by: Robert

    Thank you very much. This code is exactly what I've been looking for. There is not enough sample code for Outlook out there.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds