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

Last reviewed: November 21, 1997
Article ID: Q173056
The information in this article applies to:
  • Collaboration Data Objects (CDO), version 1.1

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 Active Messaging 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          : ActMsg
Version           : WINDOWS:1.1
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.