Click here to Skip to main content
15,879,613 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.6K   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
cp758017-Jun-04 6:56
cp758017-Jun-04 6:56 
Questionwhy I get this warming?? Pin
sysmatrix29-Jan-04 5:40
sysmatrix29-Jan-04 5:40 
AnswerRe: why I get this warming?? Pin
aspro07723-Apr-04 0:18
aspro07723-Apr-04 0:18 
AnswerRe: why I get this warming?? Pin
riki_risnandar25-Mar-05 6:46
riki_risnandar25-Mar-05 6:46 
QuestionHow can I Logon with another profile Pin
jackport7710-Nov-03 23:57
jackport7710-Nov-03 23:57 
QuestionHow do i close the Outlook App instance? Pin
Nick Pasko28-Oct-03 22:24
Nick Pasko28-Oct-03 22:24 
AnswerRe: How do i close the Outlook App instance? Pin
Deepesh Dhapola30-Oct-03 8:19
Deepesh Dhapola30-Oct-03 8:19 
GeneralRe: How do i close the Outlook App instance? Pin
Kingwulf30-Oct-03 13:56
Kingwulf30-Oct-03 13:56 
GeneralRe: How do i close the Outlook App instance? Pin
bigwizard7-Mar-04 8:14
bigwizard7-Mar-04 8:14 
Generalyour code didn't go through all contacts! Pin
darwinw20-Oct-03 15:00
darwinw20-Oct-03 15:00 
GeneralRe: your code didn't go through all contacts! Pin
Deepesh Dhapola20-Oct-03 22:40
Deepesh Dhapola20-Oct-03 22:40 
GeneralRe: your code didn't go through all contacts! Pin
darwinw21-Oct-03 8:35
darwinw21-Oct-03 8:35 
GeneralRe: your code didn't go through all contacts! Pin
madkoala21-Nov-05 1:12
madkoala21-Nov-05 1:12 
GeneralRe: your code didn't go through all contacts! Pin
Atony Chen16-Jan-06 21:23
Atony Chen16-Jan-06 21:23 
QuestionHow to write contacts infor back to Outlook?? Pin
jamestao11-Sep-03 20:42
jamestao11-Sep-03 20:42 
AnswerRe: How to write contacts infor back to Outlook?? Pin
Deepesh Dhapola13-Sep-03 0:43
Deepesh Dhapola13-Sep-03 0:43 
GeneralRe: How to write contacts infor back to Outlook?? Pin
jamestao13-Sep-03 1:28
jamestao13-Sep-03 1:28 
GeneralRe: How to write contacts infor back to Outlook?? Pin
Deepesh Dhapola19-Sep-03 1:29
Deepesh Dhapola19-Sep-03 1:29 
GeneralRe: How to write contacts infor back to Outlook?? Pin
pyranha7-Oct-03 2:18
pyranha7-Oct-03 2:18 
GeneralRe: How to write contacts infor back to Outlook?? Pin
Deepesh Dhapola20-Oct-03 22:43
Deepesh Dhapola20-Oct-03 22:43 
GeneralRe: How to write contacts infor back to Outlook?? Pin
Anonymous17-Feb-04 12:23
Anonymous17-Feb-04 12:23 
GeneralRe: How to write contacts infor back to Outlook?? Pin
Anonymous20-Feb-04 4:06
Anonymous20-Feb-04 4:06 
QuestionHow do we get the current active view of the outlook through object model? Pin
Kingwulf24-Jun-03 8:17
Kingwulf24-Jun-03 8:17 
AnswerRe: How do we get the current active view of the outlook through object model? Pin
Deepesh Dhapola13-Sep-03 1:13
Deepesh Dhapola13-Sep-03 1:13 
QuestionWhat is the value of &quot;Application&quot;? Pin
Kingwulf12-Jun-03 14:18
Kingwulf12-Jun-03 14:18 

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.