Click here to Skip to main content
15,888,610 members
Articles / Desktop Programming / ATL
Article

Importing contacts from Outlook

Rate me:
Please Sign up or sign in to vote.
4.74/5 (19 votes)
20 Feb 2003 714.9K   5.7K   83   121
Exmaple source code to import items from Outlook using the Office/Outlook Object Model.

Sample Image - maximum width is 600 pixels

Introduction

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 for this? Since 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...

C++
//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");
C++
//For Office 2000 
#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");
C++
//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());
}

This sample imports contact 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 a Calendar folder one just needs to make an object of _AppointmentItemPtr smart pointer class instead of _ContactItemPtr.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
www.d2labs.com
blogs.d2labs.com

Comments and Discussions

 
GeneralRe: Unable to instantiate Outlook Pin
riki_risnandar8-Jan-06 16:02
riki_risnandar8-Jan-06 16:02 
GeneralRe: Unable to instantiate Outlook Pin
adityasen14-Dec-09 18:45
adityasen14-Dec-09 18:45 
GeneralOnly one contact Pin
Rafael de Pablo9-Dec-04 11:15
Rafael de Pablo9-Dec-04 11:15 
GeneralA little Query Pin
ThatsAlok27-Sep-04 3:40
ThatsAlok27-Sep-04 3:40 
QuestionHow To deal with Attachment Pin
ThatsAlok27-Sep-04 3:29
ThatsAlok27-Sep-04 3:29 
GeneralRe: Outlook 2003 Problem Pin
Anonymous7-Sep-04 0:36
Anonymous7-Sep-04 0:36 
QuestionHow to identify the mail ids while fetching from the mail object Pin
Koundinya23-Aug-04 2:56
Koundinya23-Aug-04 2:56 
QuestionHow to save the outlook mail as text file Pin
Koundinya23-Aug-04 2:51
Koundinya23-Aug-04 2:51 
GeneralWhere to find MS Outlook C++ Reference Pin
anderslundsgard11-Jul-04 22:45
anderslundsgard11-Jul-04 22:45 
Questionenumerating accounts? Pin
kanetheterrible116-Jun-04 18:49
kanetheterrible116-Jun-04 18:49 
GeneralOutlook 2003 Problem Pin
Mike Beckerleg30-Apr-04 0:44
Mike Beckerleg30-Apr-04 0:44 
Generalmaking this application a Dynamic Link Librarie Pin
aspro07722-Apr-04 23:36
aspro07722-Apr-04 23:36 
GeneralRe: making this application a Dynamic Link Librar Pin
aspro07722-Apr-04 23:45
aspro07722-Apr-04 23:45 
GeneralRe: making this application a Dynamic Link Librarie Pin
aspro07723-Apr-04 0:15
aspro07723-Apr-04 0:15 
GeneralObject Model Guard for Outlook 2000 Service Pack 2 and later versions Pin
Anthony_Yio21-Apr-04 17:27
Anthony_Yio21-Apr-04 17:27 
GeneralRe: Object Model Guard for Outlook 2000 Service Pack 2 and later versions Pin
Deepesh Dhapola21-Apr-04 19:05
Deepesh Dhapola21-Apr-04 19:05 
GeneralRe: Object Model Guard for Outlook 2000 Service Pack 2 and later versions Pin
Mike Beckerleg29-Apr-04 23:17
Mike Beckerleg29-Apr-04 23:17 
GeneralAppointments Objects Pin
Rodrigo Vaz8-Apr-04 4:26
Rodrigo Vaz8-Apr-04 4:26 
GeneralAbout certificate in Outlook Contact Pin
Roy Zuo21-Mar-04 20:54
Roy Zuo21-Mar-04 20:54 
GeneralUnable to instantiate Outlook under Console Pin
WindSpring20-Mar-04 20:21
WindSpring20-Mar-04 20:21 
GeneralRe: Unable to instantiate Outlook under Console Pin
Deepesh Dhapola21-Mar-04 22:43
Deepesh Dhapola21-Mar-04 22:43 
GeneralRe: Unable to instantiate Outlook under Console Pin
J W14-May-04 2:48
J W14-May-04 2:48 
GeneralRe: Unable to instantiate Outlook under Console Pin
Deepesh Dhapola14-May-04 6:12
Deepesh Dhapola14-May-04 6:12 
GeneralUnable to instantiate Outlook Pin
alex.barylski30-Jan-04 23:48
alex.barylski30-Jan-04 23:48 
GeneralRe: Unable to instantiate Outlook Pin
alex.barylski31-Jan-04 0:05
alex.barylski31-Jan-04 0:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.