Using CDO to Retrieve the Server Name

When the LitCrit Outlook form is opened, the ChooseTitle method of the client-side Critique COM component is called. One parameter in this call is the name of the computer on which Microsoft Exchange Server and the CML/LitCrit application are installed. First, the application uses CDO to retrieve this server.

To gain access to the objects and methods of CDO, the application first needs to create a CDO Session object. It does this through the CreateObject method of the Application object, the top-level object in the Outlook object model. In the following code, after the CDO Session object is created, the application logs on to the existing MAPI session:

Set objCDOSession = Application.CreateObject("MAPI.Session")
objCDOSession.LogOn "",False,False,False 'ProfileName,ProfilePassword,ShowDialog,Existing Session

Note  The CreateItem method of the Application object is used to instantiate standard MAPI messaging objects (such as IPM.Note and IPM.Post messages), not general COM objects; for COM objects, use the CreateObject method.

Location of the Server Name Value

The name of the Microsoft Exchange Server computer is stored as a MAPI property in the Fields collection of every folder object in the information store of the server. This means that to find the server name, you can open any folder and read its Fields collection. Every information store has a folder that must be present and cannot be renamed: the Inbox. This reliability makes the Inbox a logical folder to use to perform this task.

Finding the MAPI Property Tag

To find a specific field (such as Server Name) within the Fields collection, use its MAPI property tag. This is a number that was obtained empirically, by stepping through the loop shown in Reading Field Names, using a special debug mode that displays the Field.ID property, and the Field.Name and Field.Value properties, when present (not all fields have an associated Name property). The F & M developer observed these values until the name of the server known to be in use was seen. The developer then noted the corresponding number for the Field.ID. This is the server name's MAPI property tag.

The following utility code was written to obtain these values:

For Each Field in objCDOSession.Inbox.Fields
  If Field.Name = "" Then 
     ' for standard MAPI properties:
     Msgbox "Field.ID: " & Field.ID & " =  " & Field.Value 
  Else 
     ' for user-defined MAPI properties:
     Msgbox "Field.Name: " & Field.Name & "= " & Field.Value
  End If
Next

In the preceding loop, a message box displays either the Field.ID and Field.Value of a standard MAPI property (which has no associated Name field), or the Field.Name and Field.Value of a user-defined MAPI property.

Reading Field Names

The property tag for the server name field is assigned to a constant at the beginning of the EnhancedLitCrit script:

Const MAPI_PROPTAG_SERVERNAME = &H6644001E   'PropID For Server Name

At run time, this constant is used in a loop to locate and read the field holding the server name:

For Each Field in objCDOSession.Inbox.Fields
   If Field.ID = MAPI_PROPTAG_SERVERNAME  Then 
      ServerName = Field.Value
      Exit For
   End If
Next
objCDOSession.Logoff

The CDO Session object is the topmost object in the CDO object model, and the Inbox object is also a method of the Session object. Another of its methods, Logoff, is called after exiting the loop to end the CDO session.

Using the Server Name

The Exchange server name is used later in the script, for example in the following line, which calls a method of the client-side Critique COM component, passing the server name as a parameter:

objCritique.ChooseTitle BibNo, Title, Authors, MediaType, ObjectID, _
                              CStr(LogOn), CStr(ServerName)