How to use an Outlook Object Model from Visual C++ by using a #import statement

This article was previously published under Q259298
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how to use Microsoft Outlook Object Model from Visual C++ through the #import statement and smart pointers.
The #import statement is different for each version of the Microsoft Outlook Object Library (Msoutlxx.olb).

The following code is an example of an #import statement that works with all versions of the Microsoft Outlook Object Model:
// Define this according to the Outlook Object// Model version you are compiling under#define OUTL11	// Outlook 2003//#define OUTL10	// Outlook 2002//#define OUTL9		// Outlook 2000#pragma warning(disable:4146)#if defined(OUTL11) // Outlook 2003	#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\mso.dll" \		no_namespace	#import "C:\Program Files\Microsoft Office\OFFICE11\msoutl.olb" \		rename_namespace("Outlook")#elif defined(OUTL10) // Outlook 2002	#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE10\mso.dll" \		no_namespace, rename("DocumentProperties", "DocProps")	#import "C:\Program Files\Microsoft Office\OFFICE10\msoutl.olb" \		rename_namespace("Outlook")#elif defined(OUTL9) // Outlook 2000	#import "C:\Program Files\Microsoft Office\Office\mso9.dll" \		no_namespace, rename("DocumentProperties", "DocProps")	#import "C:\Program Files\Microsoft Office\Office\msoutl9.olb" \		rename_namespace("Outlook")#endif // OUTL11#pragma warning(default:4146)using namespace Outlook;
Note You must change the paths for the system, and then build your application by using the earliest version of Microsoft Outlook to avoid backward compatibility issues.

The following code is an example of how to log on to your default profile, and then print the number of messages in the Inbox folder. The following code also demonstrates how you can use the #import statements from the previous code by using smart pointers.
_ApplicationPtr spApp("Outlook.Application");  _NameSpacePtr pMAPI = spApp->GetNamespace("MAPI");  pMAPI->Logon("","",false,false);  MAPIFolderPtr InboxFolder = pMAPI->GetDefaultFolder(olFolderInbox);      _ItemsPtr Items = InboxFolder->GetItems();	  printf("Total number of messages in Inbox - %d \n", Items->Count);
After you compile the project, four files are created for you by the #import statement: MsoXX.tli, MsoXX.tlh, MsoutlXX.tli, and MsoutlXX.tlh. If you manually add these files to your project, you can view all the Office and Outlook library interfaces in the ClassView class, and the IntelliSense option displays the parameters as you type the interface methods.

Article ID: 259298 - Last Review: 12/05/2015 19:35:45 - Revision: 3.3

Microsoft Office Outlook 2003, Microsoft Outlook 2002 Standard Edition, Microsoft Outlook 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto kbmsg kboutlookobj KB259298