Microsoft KB Archive/171638

From BetaArchive Wiki
Knowledge Base

How To Use CDO (1.x) to Work with Public/Personal Folders

Article ID: 171638

Article Last Modified on 6/6/2005


  • Microsoft Collaboration Data Objects 1.21
  • Microsoft Collaboration Data Objects 1.1
  • Microsoft Collaboration Data Objects 1.2
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition

This article was previously published under Q171638


To navigate to folders using Collaboration Data Objects (CDO 1.x) library, you need to know either the FolderID or the name of the folder and where it is. This example assumes that you do not know the FolderID, but that you do know what folder you are looking for, and where it is located. For purposes of demonstration, this example assumes that the folder you are looking for is in your local PST (Personal Folders).


To navigate to a folder that is not built into the session object (that is, Inbox or Outbox), you need to know something about the folder. For example, if you know the FolderID, then you can use the GetFolder method of the session object. The problem with this method is that you do not always know the FolderID, which is a string that specifies the unique identifier of the folder.

A folder's unique identifier comes from MAPI, which assigns a permanent, unique identifier when an object is created. This identifier does not change from one MAPI session to another, nor from one messaging domain to another.

If you know only the folder's location and name, you must navigate to the folder. One way to accomplish this is to iterate through the InfoStores until you find the one that contains your folder. Then iterate through the folders in the InfoStore to find your target folder.

Each InfoStore contains a RootFolder. By identifying the RootFolder's name, you know what type of InfoStore you are in. Use the following table to determine where in the hierarchy you are:

   Public Folders            IPM_SUBTREE
   Mailbox - <User Name>     Top of Information Store
   PST                       Top of Personal Folders

Using the above table, you know that if you want to find the folder named "Messaging" in the PST, you need to find a TopFolder named "Top of Personal Folders". Then, navigate through these folders until you find the "Messaging" folder.

One problem with this method is that you can have multiple PST files in a profile. If this is the case, you need to employ an additional check to make sure you are in the correct PST. Alternatively, you can check for the name of the InfoStore instead of the name of the Root Folder.

The following code demonstrates how to use this technique.

This code sample requires a reference to the "Microsoft Active Messaging 1.1 Object Library" (Olemsg32.dll) or to the Microsoft CDO 1.2 (or later) library. This sample assumes that there is an active client session running.

   Option Explicit
   Dim objSession As MAPI.Session

   Private Sub Form_Load()
      Set objSession = CreateObject("MAPI.Session")
      objSession.Logon showdialog:=False, newsession:=False
   End Sub
   Private Sub Command1_Click()
      Dim objPSTFolder As Folder
      Dim objMessages As Messages
      Dim objOneMessage As Message

      'Want to find the Messaging folder in my PST. Modify the "Messaging"
      'as needed to find the folder you are interested in.
      Set objPSTFolder = objFindTargetFolder("Top of Personal Folders", _
      Set objMessages = objPSTFolder.Messages
      Set objOneMessage = objMessages.GetFirst
   End Sub
   Private Function objFindTargetFolder( _
      strTargetTopFolder As String, _
      strSearchName As String) As Folder

      Dim objInfoStores As InfoStores
      Dim objInfoStore As InfoStore
      Dim objTopFolder As Folder
      Dim objPSTFolders As Folders
      Dim i As Integer

      Set objInfoStores = objSession.InfoStores
      'This message box loop is just telling you what the current
      'objects are. It is not needed for the function to work
      For i = 1 To objInfoStores.Count
         Set objInfoStore = objInfoStores(i)
         Set objTopFolder = objInfoStore.RootFolder
         MsgBox "i= " & i & Chr(10) & _
              "InfoStore.Name= " & objInfoStores(i).Name & Chr(10) & _
              "TopFolder.Name= " & objTopFolder.Name
      Next i

      'This loop finds the TopFolder you specified.
      For i = 1 To objInfoStores.Count
         Set objInfoStore = objInfoStores(i)
         Set objTopFolder = objInfoStore.RootFolder
         If objTopFolder.Name = strTargetTopFolder Then 'Found PST
            'Because you can have more than one PST in a profile,
            'you may want to put another check here to make sure you have
            'the correct PST. This check would need to specify a string
            'that is the name of the PST you are looking for.
            'It would look something like this:
            'If objInfoStore.Name = "MyPST" Then 'Found own PST
            '   Exit For
            'End If
         End If
      Next i
      Set objPSTFolders = objTopFolder.Folders
      For i = 1 To objPSTFolders.Count
         MsgBox objPSTFolders.Item(i).Name
         If objPSTFolders.Item(i).Name = strSearchName Then
            Exit For
         End If
      Next i
      Set objFindTargetFolder = objPSTFolders.Item(i)
      Set objTopFolder = Nothing
      Set objPSTFolders = Nothing
      Set objInfoStores = Nothing
      Set objInfoStore = Nothing
   End Function

Keywords: kbhowto kbmsg KB171638