Click here to Skip to main content
15,903,175 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 718.7K   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: Outlook alert message Pin
Deepesh Dhapola13-Sep-03 0:52
Deepesh Dhapola13-Sep-03 0:52 
GeneralRe: Outlook alert message Pin
darwinw21-Oct-03 8:42
darwinw21-Oct-03 8:42 
GeneralRe: Outlook alert message Pin
wangsongshan12-Sep-05 14:59
wangsongshan12-Sep-05 14:59 
GeneralRe: Outlook alert message Pin
wangsongshan12-Sep-05 15:00
wangsongshan12-Sep-05 15:00 
GeneralRe: Outlook alert message Pin
canido14-Jun-07 6:34
canido14-Jun-07 6:34 
GeneralRe: Outlook alert message Pin
canido14-Jun-07 23:26
canido14-Jun-07 23:26 
GeneralRe: Outlook alert message Pin
harishksh3-Sep-07 3:48
harishksh3-Sep-07 3:48 
AnswerRe: Outlook alert message -- SOLUTION! [modified] Pin
Member 60417929-Jul-08 13:04
Member 60417929-Jul-08 13:04 
here's what you're looking for (and what i was looking for and found):

http://www.codeproject.com/KB/IP/CMapiEx.aspx[^]

that's a working sample of how to use extend mapi.

when you iterate through the contacts in this code, call MAPIOBJECT on the contact, then through that get the email and name of the contact to get around the security warning.

here's the quick and dirty (didn't include error checking):

static const GUID MY_IID_IMAPIProp = // {00020303-0000-0000-C000-000000000046}
{0x00020303,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
static const GUID OUTLOOK_EMAIL_DATA = // {00062004-0000-0000-C000-000000000046}
{0x00062004,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
#define OUTLOOK_EMAIL1_TYPE 0x8082
#define OUTLOOK_EMAIL1_VALUE 0x8083
#define OUTLOOK_EMAIL1_NAME 0x8084

#include <mapix.h> // for extended mapi
#include <mapiutil.h> // for get one prop
#pragma comment (lib, "mapi32.lib")

...

CComPtr<IUnknown> spMAPIObj = NULL;
CComPtr<IMAPIProp> spMAPIProp = NULL;

spMAPIObj = spContact->GetMAPIOBJECT();
hr = spMAPIObj->QueryInterface(MY_IID_IMAPIProp, (void **)&spMAPIProp );

LPSPropValue pProp = NULL;
hr = HrGetOneProp(spMAPIProp, PR_DISPLAY_NAME, &pProp);
//0x00040380 == undefined prop, but is success, so explicity check for S_OK
if (hr == S_OK && pProp != NULL && pProp->Value.LPSZ != NULL) {
sVal = prop.Value.LPSZ;
}
MAPIFreeBuffer(pProp);

ULONG ulPropCount = 0;
MAPINAMEID nameID = { (GUID*)&OUTLOOK_EMAIL_DATA, MNID_ID, OUTLOOK_EMAIL1_VALUE };
LPMAPINAMEID lpNameID[1]={ &nameID };
LPSPropTagArray lppPropTags = NULL;

hr = pIMAPIProp->GetIDsFromNames(1, lpNameID, 0, &lppPropTags);

hr = pIMAPIProp->GetProps(lppPropTags, fMapiUnicode, &ulPropCount, &pProp);
//0x00040380 == undefined prop, but is success, so explicity check for S_OK
if (hr == S_OK &amp;&amp; pProp != NULL &amp;&amp; pProp-&gt;Value.LPSZ != NULL) {
sVal = prop.Value.LPSZ;
}

MAPIFreeBuffer(lppPropTags);
MAPIFreeBuffer(pProp);


Dan.

modified on Thursday, July 31, 2008 2:23 PM

GeneralOutlook GetNext Does Not Work Right Pin
Anonymous18-Apr-03 20:16
Anonymous18-Apr-03 20:16 
GeneralRe: Outlook GetNext Does Not Work Right Pin
Anonymous17-Oct-03 1:19
Anonymous17-Oct-03 1:19 
Generalthe diference Pin
Anonymous13-Mar-03 12:40
Anonymous13-Mar-03 12:40 
GeneralRe: the diference Pin
Deepesh Dhapola17-Mar-03 5:55
Deepesh Dhapola17-Mar-03 5:55 
Questionhow can i fetch messages from Outlook Express? Pin
coolvcguy28-Feb-03 18:22
coolvcguy28-Feb-03 18:22 
AnswerRe: how can i fetch messages from Outlook Express? Pin
Member 2680916-Mar-03 2:55
Member 2680916-Mar-03 2:55 
AnswerRe: how can i fetch messages from Outlook Express? Pin
cgabi7-Mar-03 6:06
cgabi7-Mar-03 6:06 
GeneralRe: how can i fetch messages from Outlook Express? Pin
coolvcguy7-Mar-03 18:06
coolvcguy7-Mar-03 18:06 
AnswerRe: how can i fetch messages from Outlook Express? Pin
Roman Borik3-Sep-03 21:00
Roman Borik3-Sep-03 21:00 
AnswerRe: how can i fetch messages from Outlook Express? Pin
Deepesh Dhapola19-Sep-03 1:57
Deepesh Dhapola19-Sep-03 1:57 
GeneralRe: how can i fetch messages from Outlook Express? Pin
coolvcguy19-Sep-03 5:23
coolvcguy19-Sep-03 5:23 
AnswerAbout fetching messages from Outlook Express - partial solution Pin
Roman Borik24-Sep-03 0:41
Roman Borik24-Sep-03 0:41 
GeneralRe: About fetching messages from Outlook Express - partial solution Pin
coolvcguy24-Sep-03 5:58
coolvcguy24-Sep-03 5:58 
GeneralRe: Name Verification!!! Pin
Deepesh Dhapola17-Mar-03 5:48
Deepesh Dhapola17-Mar-03 5:48 
GeneralThe implementation can be done in other way also Pin
cgabi21-Feb-03 5:18
cgabi21-Feb-03 5:18 
GeneralRe: The implementation can be done in other way also Pin
Miguel Lopes21-Feb-03 7:56
Miguel Lopes21-Feb-03 7:56 
GeneralRe: The implementation can be done in other way also Pin
cgabi21-Feb-03 23:26
cgabi21-Feb-03 23:26 

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.