HOWTO: Retrieve MAPI User Information with OLE Messaging

ID: Q154514

The information in this article applies to:

SUMMARY

The OLE Messaging Library can be used to gain access to MAPI address book information. This article provides a simple example that shows how to retrieve details of a mail user via the OLE Messaging interface.

MORE INFORMATION

The Microsoft OLE Messaging Library exposes messaging objects for use by Microsoft Visual Basic and Microsoft Visual C++ applications. The Microsoft OLE Messaging Library does not represent a new messaging model. It represents an additional interface to the Messaging Application Programming Interface (MAPI) model, designed to handle the most common tasks for client developers using Visual Basic and Visual C++.

To use the example in this article, you will need to have a MAPI-compliant mail system installed and the OLE Messaging Library that comes with Microsoft Exchange 4.0 or later and also as part of the Microsoft Developer Network.

Step-by Step Example

1. Start a new project. Form1 is created by default.

2. Place a CommandButton on Form1.

3. Add the following code to the General Declarations section of Form1:

      Option Explicit

      Private Const PR_BUSINESS_TELEPHONE_NUMBER As Long = &H3A08001E
      Private Const PR_OFFICE_LOCATION As Long = &H3A19001E
      Private Const PR_DEPARTMENT_NAME As Long = &H3A18001E
      Private Const PR_COMPANY_NAME As Long = &H3A16001E

      Sub Show_Details(sUserEmailName As String)
        Dim objsession As Object
        Dim objMessage As Object
        Dim objUser As Object
        Dim objOneRecip As Object
        Dim addrentry As Object
        Screen.MousePointer = vbHourglass
        Set objsession = CreateObject("MAPI.Session")
        objsession.Logon
        Set objMessage = objsession.Outbox.Messages.Add
        Set objOneRecip = objMessage.Recipients.Add
        objOneRecip.Name = sUserEmailName
        objOneRecip.Type = 1
        objOneRecip.Resolve
        Set addrentry = objOneRecip.addressentry
        With addrentry.Fields
          Print "Phone: " & .Item(PR_BUSINESS_TELEPHONE_NUMBER).Value
          Print "Office: " & .Item(PR_OFFICE_LOCATION).Value
          Print "Full Name: " & objOneRecip.Name
          Print "Department: " & .Item(PR_DEPARTMENT_NAME).Value
          Print "Company:" & .Item(PR_COMPANY_NAME).Value
        End With
        Screen.MousePointer = vbArrow
      End Sub

      Private Sub Command1_Click()
        Show_Details "Johndoe"
      End Sub

4. Press the F5 key to run the project. The information for the specified
   user will be printed on the form. The sUserEmailName parameter in the
   Show_Details routine can be the full name or the alias for the user.

REFERENCES

OLE Messaging is fully documented in the Win32 SDK, available on the Microsoft Developer Network (MSDN).

The Mastering Series for Exchange Development, also available from Microsoft, has extensive Visual Basic code samples and explanations relating to OLE Messaging.

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

Keywords          : kbCDO110 kbCDO120 kbCDO121 kbOLEMsg kbVBp400 kbVBp500 kbGrpMsg 
Version           : WINDOWS:4.0,5.0,1.21
Platform          : NT WINDOWS
Issue type        : kbhowto

Last Reviewed: April 8, 1999