Click here to Skip to main content
13,866,638 members
Click here to Skip to main content
Add your own
alternative version

Stats

466.5K views
5.5K downloads
83 bookmarked
Posted 20 Feb 2003
Licenced

Importing contacts from Outlook

, 20 Feb 2003
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Deepesh Dhapola
Web Developer
India India
www.d2labs.com
blogs.d2labs.com

You may also be interested in...

Comments and Discussions

 
AnswerRe: Outlook alert message -- SOLUTION! [modified] Pin
Member 60417929-Jul-08 14:04
memberMember 60417929-Jul-08 14:04 
GeneralOutlook GetNext Does Not Work Right Pin
Anonymous18-Apr-03 21:16
memberAnonymous18-Apr-03 21:16 
GeneralRe: Outlook GetNext Does Not Work Right Pin
Anonymous17-Oct-03 2:19
memberAnonymous17-Oct-03 2:19 
Generalthe diference Pin
Anonymous13-Mar-03 13:40
memberAnonymous13-Mar-03 13:40 
GeneralRe: the diference Pin
Deepesh Dhapola17-Mar-03 6:55
memberDeepesh Dhapola17-Mar-03 6:55 
Questionhow can i fetch messages from Outlook Express? Pin
coolvcguy28-Feb-03 19:22
membercoolvcguy28-Feb-03 19:22 
AnswerRe: how can i fetch messages from Outlook Express? Pin
Member 2680916-Mar-03 3:55
memberMember 2680916-Mar-03 3:55 
AnswerRe: how can i fetch messages from Outlook Express? Pin
cgabi7-Mar-03 7:06
membercgabi7-Mar-03 7:06 
hi,

You can use SimpleMAPI for fetching messages. But it has a big limitation: you'll be able to fetch just for inbox.

Do you think that will be usefull to write an article and a sample app for this ?

Thanks,
Gabi
GeneralRe: how can i fetch messages from Outlook Express? Pin
coolvcguy7-Mar-03 19:06
membercoolvcguy7-Mar-03 19:06 
AnswerRe: how can i fetch messages from Outlook Express? Pin
Roman Borik3-Sep-03 22:00
memberRoman Borik3-Sep-03 22:00 
AnswerRe: how can i fetch messages from Outlook Express? Pin
Deepesh Dhapola19-Sep-03 2:57
memberDeepesh Dhapola19-Sep-03 2:57 
GeneralRe: how can i fetch messages from Outlook Express? Pin
coolvcguy19-Sep-03 6:23
membercoolvcguy19-Sep-03 6:23 
AnswerAbout fetching messages from Outlook Express - partial solution Pin
Roman Borik24-Sep-03 1:41
memberRoman Borik24-Sep-03 1:41 
GeneralRe: About fetching messages from Outlook Express - partial solution Pin
coolvcguy24-Sep-03 6:58
membercoolvcguy24-Sep-03 6:58 
GeneralRe: Name Verification!!! Pin
Deepesh Dhapola17-Mar-03 6:48
memberDeepesh Dhapola17-Mar-03 6:48 
GeneralThe implementation can be done in other way also Pin
cgabi21-Feb-03 6:18
membercgabi21-Feb-03 6:18 
GeneralRe: The implementation can be done in other way also Pin
Miguel Lopes21-Feb-03 8:56
memberMiguel Lopes21-Feb-03 8:56 
GeneralRe: The implementation can be done in other way also Pin
cgabi22-Feb-03 0:26
membercgabi22-Feb-03 0:26 
GeneralRe: The implementation can be done in other way also Pin
Heath Stewart23-Feb-03 5:47
protectorHeath Stewart23-Feb-03 5:47 
GeneralRe: The implementation can be done in other way also Pin
Member 2680917-Mar-03 1:39
memberMember 2680917-Mar-03 1:39 
GeneralRe: The implementation can be done in other way also Pin
cgabi7-Mar-03 7:04
membercgabi7-Mar-03 7:04 
GeneralRe: The implementation can be done in other way also Pin
toddor_sturt28-Dec-05 16:40
membertoddor_sturt28-Dec-05 16:40 
GeneralImporting Typelibs PinPopular
Heath Stewart20-Feb-03 19:17
protectorHeath Stewart20-Feb-03 19:17 
GeneralRe: Importing Typelibs Pin
andyj11520-Feb-03 21:35
memberandyj11520-Feb-03 21:35 
GeneralRe: Importing Typelibs Pin
Heath Stewart21-Feb-03 4:21
protectorHeath Stewart21-Feb-03 4:21 

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.

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