Importing contacts from Outlook | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 20, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.