Click here to Skip to main content
15,880,796 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 709.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

 
Questionvb.net Pin
Member 984981522-Feb-13 0:50
Member 984981522-Feb-13 0:50 
QuestionAny Chance of getting the source in C# or vb.net? Pin
99pshop24-Jan-11 10:41
99pshop24-Jan-11 10:41 
QuestionNeed to Import e-mail attachments to C# program Pin
Kasunmit15-May-10 17:29
Kasunmit15-May-10 17:29 
GeneralCheck the alias Pin
fbi56fbi6-Sep-09 22:14
fbi56fbi6-Sep-09 22:14 
QuestionProblem with Outlook 2007 Pin
raulhmacias15-Apr-09 14:40
raulhmacias15-Apr-09 14:40 
QuestionRe: Problem with Outlook 2007 Pin
andkeller15-Jun-09 20:52
andkeller15-Jun-09 20:52 
AnswerRe: Problem with Outlook 2007 Pin
rbrunton27-Jul-09 1:14
rbrunton27-Jul-09 1:14 
AnswerRe: Problem with Outlook 2007 Pin
hufubin13-Nov-09 20:01
hufubin13-Nov-09 20:01 
AnswerRe: Problem with Outlook 2007 Pin
Marcus Chuang13-Oct-10 0:49
Marcus Chuang13-Oct-10 0:49 
Generalgetting mails from msoutlook Pin
alexenoy16-Mar-06 0:48
alexenoy16-Mar-06 0:48 
QuestionHow do I do all this using C# Pin
NewbieDude15-Mar-06 21:05
NewbieDude15-Mar-06 21:05 
QuestionHow can I get images from e-mail message? Pin
Zahid Younas6-Jan-06 19:38
Zahid Younas6-Jan-06 19:38 
QuestionHow can I get web-page property using MAPI Pin
toddor_sturt28-Dec-05 15:42
toddor_sturt28-Dec-05 15:42 
GeneralAccessing other user's Outlook folders Pin
Kokas1-Nov-05 2:19
Kokas1-Nov-05 2:19 
QuestionHOW TO don't show waring Dialog? Pin
wangsongshan11-Sep-05 19:30
wangsongshan11-Sep-05 19:30 
AnswerRe: HOW TO don't show waring Dialog? Pin
Deepesh Dhapola11-Sep-05 19:36
Deepesh Dhapola11-Sep-05 19:36 
GeneralRe: HOW TO don't show waring Dialog? Pin
goebish26-Apr-06 22:24
goebish26-Apr-06 22:24 
GeneralRe: HOW TO don't show waring Dialog? Pin
canido14-Jun-07 23:24
canido14-Jun-07 23:24 
Answer-- HERE IS HOW TO GET AROUND THE WARNING -- Pin
Member 60417929-Jul-08 12:43
Member 60417929-Jul-08 12:43 
QuestionRe: HOW TO don't show waring Dialog? Pin
licj200821-Jul-07 23:31
licj200821-Jul-07 23:31 
GeneralImporting messages Pin
SloanCode15-Aug-05 0:31
SloanCode15-Aug-05 0:31 
GeneralOutLook2003 security problem Pin
Member 10491106-Apr-05 20:56
Member 10491106-Apr-05 20:56 
GeneralUnable to instantiate Outlook Pin
riki_risnandar19-Mar-05 17:19
riki_risnandar19-Mar-05 17:19 
GeneralRe: Unable to instantiate Outlook Pin
bharadwajgangadhar5-Oct-05 4:25
bharadwajgangadhar5-Oct-05 4:25 
GeneralRe: Unable to instantiate Outlook Pin
Zahid Younas6-Jan-06 0:58
Zahid Younas6-Jan-06 0:58 

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.