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

Article translations Article translations
Article ID: 259298 - View products that this article applies to.
This article was previously published under Q259298
Expand all | Collapse all

SUMMARY

This article describes how to use Microsoft Outlook Object Model from Visual C++ through the #import statement and smart pointers.

MORE INFORMATION

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.

Properties

Article ID: 259298 - Last Review: September 9, 2005 - Revision: 3.3
APPLIES TO
  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Outlook 2000 Standard Edition
Keywords: 
kbhowto kbmsg kboutlookobj KB259298

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com