Click here to Skip to main content
Licence 
First Posted 20 Feb 2003
Views 228,939
Downloads 3,738
Bookmarked 81 times

Importing contacts from Outlook

By | 20 Feb 2003 | Article
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...

//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 "E:\Program Files\Common Files\Microsoft Shared\Office10\mso.dll" named_guids
#import "E:\Microsoft Office\Office10\MSOUTL.OLB" \ no_namespace
    exclude("_IRecipientControl", "_DRecipientControl");
//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

About the Author

Deepesh Dhapola

Web Developer

India India

Member

www.d2labs.com
blogs.d2labs.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAny Chance of getting the source in C# or vb.net? Pinmember99pshop10:41 24 Jan '11  
QuestionNeed to Import e-mail attachments to C# program PinmemberKasunmit17:29 15 May '10  
GeneralCheck the alias Pinmemberfbi56fbi22:14 6 Sep '09  
QuestionProblem with Outlook 2007 Pinmemberraulhmacias14:40 15 Apr '09  
QuestionRe: Problem with Outlook 2007 Pinmemberandkeller20:52 15 Jun '09  
AnswerRe: Problem with Outlook 2007 Pinmemberrbrunton1:14 27 Jul '09  
AnswerRe: Problem with Outlook 2007 Pinmemberhufubin20:01 13 Nov '09  
AnswerRe: Problem with Outlook 2007 PinmemberMember 38342650:49 13 Oct '10  
Generalgetting mails from msoutlook Pinmemberalexenoy0:48 16 Mar '06  
QuestionHow do I do all this using C# PinmemberNewbieDude21:05 15 Mar '06  
QuestionHow can I get images from e-mail message? PinmemberZahid Younas19:38 6 Jan '06  
QuestionHow can I get web-page property using MAPI Pinmembertoddor_sturt15:42 28 Dec '05  
GeneralAccessing other user's Outlook folders PinmemberKokas2:19 1 Nov '05  
QuestionHOW TO don't show waring Dialog? Pinmemberwangsongshan19:30 11 Sep '05  
AnswerRe: HOW TO don't show waring Dialog? PinmemberDeepesh Dhapola19:36 11 Sep '05  
GeneralRe: HOW TO don't show waring Dialog? Pinmembergoebish22:24 26 Apr '06  
You can override this warning with redemption.
see http://www.dimastr.com/redemption/ for details.
GeneralRe: HOW TO don't show waring Dialog? Pinmembercanido23:24 14 Jun '07  
Answer-- HERE IS HOW TO GET AROUND THE WARNING -- PinmemberMember 60417912:43 29 Jul '08  
QuestionRe: HOW TO don't show waring Dialog? Pinmemberlicj200823:31 21 Jul '07  
GeneralImporting messages PinmemberSloanCode0:31 15 Aug '05  
GeneralOutLook2003 security problem Pinmembernkzz20:56 6 Apr '05  
GeneralUnable to instantiate Outlook Pinmemberriki_risnandar17:19 19 Mar '05  
GeneralRe: Unable to instantiate Outlook Pinmemberbharadwajgangadhar4:25 5 Oct '05  
GeneralRe: Unable to instantiate Outlook PinmemberZahid Younas0:58 6 Jan '06  
GeneralRe: Unable to instantiate Outlook Pinmemberriki_risnandar16:02 8 Jan '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 21 Feb 2003
Article Copyright 2003 by Deepesh Dhapola
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid