HOWTO: Use VB with Active Messaging to Add a New Folder

ID: Q173056

The information in this article applies to:

SUMMARY

This article describes how to use the Active Messaging library to add a new folder below an existing folder in a MessageStore.

MORE INFORMATION

This Visual Basic code sample assumes a reference to the Active Messaging Library version 1.1 (OLEMSG32.DLL). For additional information on where to obtain this library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q171440
   TITLE     : INFO: Where to Acquire the Collaboration Data Objects
               Libraries

Sample Code

Paste the following code into a new Visual Basic Form:

    Dim objSession As MAPI.Session
    Dim objMsgColl As Messages
    Dim objInfoStColl As InfoStores
    Dim objInfoStore As InfoStore
    Dim objRootFolder As Folder
    Dim objCurFolder As Folder
    Dim objFoldersColl As Folders
    Dim objNewFolder As Folder

    Private Sub Form_Load()
       '1. Create and Logon to a MAPI.Session
       '------------------------------------------------------------
       Set objSession = CreateObject("mapi.session")
       objSession.Logon "YourProfileNameHere"

       '------------------------------------------------------------
       '2. Move through Folders Coll to where you will add a new Folder
       'Get your InfoStore Collection
       Set objInfoStColl = objSession.InfoStores

       'Choose one of three methods to select the desired InfoStore here.
       '  1. Pass the explicit .Name of your InfoStore as a parameter.
       '  2. Enforce a fixed naming convention on the InfoStore and check
       '     the .Name property against it. The Exchange default is
       '     "Mailbox - " with the users displayname concatenated onto
       '     the end. This method is used below.
       '  3. Check the .Name of the RootFolder of the InfoStore. These
       '     should be unique to the type of the InfoStore and is not
       '     available to the user to modify.
       Set objInfoStore = objInfoStColl.Item("Mailbox - My DisplayName")

       'Get the Root Folder of your InfoStore
       Set objRootFolder = objInfoStore.RootFolder
       Set objFoldersColl = objRootFolder.Folders

       'Determine how many subfolders there are, and find your candidate
       'new parent Folder. This sample looks for "Temp" and will add
       '"Test" as a child folder of "Temp". "Temp" must exist before this
       'will run successfully.
       cntFolders = objRootFolder.Folders.Count
       incCurFold = 1
       Set objCurFolder = objFoldersColl.GetFirst
       Do While objCurFolder.Name <> "Temp"  'Temp = Cand new parent Folder
          Set objCurFolder = objFoldersColl.GetNext
          incCurFold = incCurFold + 1
          If incCurFold = cntFolders And objCurFolder.Name <> "Temp" Then
             MsgBox "The candidate parent folder wasn't found." & vbCrLf _
                  & "Handle error appropriate to your app."
             Exit Do
          End If
       Loop

       '------------------------------------------------------------
       '3. - Add a new Folder below the current Folder
       'Assume objCurFolder is the new candidate parent Folder
       'Note - Any action other than an Add will require an .Update
       'before changes take effect.
       Set objNewFolder = objCurFolder.Folders.Add("Test")

       'Clean up and bail out
       objSession.Logoff
       Set objSession = Nothing
       Set objMsgColl = Nothing
       Set objInfoStColl = Nothing
       Set objInfoStore = Nothing
       Set objRootFolder = Nothing
       Set objCurFolder = Nothing
       Set objFoldersColl = Nothing
       Set objNewFolder = Nothing

       Unload Me

   End Sub

Please note that if you are attempting to add a Folder to the Public Folders tree, you must have "Create Subfolder" rights on the Folder within which you are attempting create the new folder.

REFERENCES

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 kbCDO121 kbMsg kbVBp kbGrpMsg 
Version           : WINDOWS:1.1, 1.21
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 8, 1999