Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)
The Folders collection represents multiple MAPIFolder objects. A Folders collection represents multiple folders, whereas a MAPIFolder object represents only a single folder. The Folders collection is accessed off of the NameSpace or the MAPIFolder objects. The following section describes the methods of the Folder collection. The only property you would use in the Folders collection is the Count property, which is self-explanatory.
Folders Collection Methods
By using the methods of the Folders collection object, you can perform the standard functions you would expect on folders, such as adding, sorting, finding, and deleting folders in the collection. Following is a description of the methods of this collection.
Add Method
By using the Add method, you can add a new folder to the current Folders collection. This method takes the parameters Name and Type for the folder. If the Type is not specified such as olContacts (10), the default type of the parent folder is used. The returned value from this method is a MAPIFolder object representing the new folder. The MAPIFolder object is discussed in detail later in this supplement. To access specific folders in the collection, you can use either the name of the folder, which is case-sensitive, or the index number of the folder in the collection. The following example shows you how you can add a new folder to the collection that corresponds to a user's mailbox. The sample assumes that the name of the user's mailbox is Mailbox – UserName. If this is not the case in your Outlook environment, you should replace that code with the name of your mailbox before running the sample.
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") oCurrentUser = oNS.CurrentUser.Name set oMAPIFolder = oNS.Folders("Mailbox - " & oCurrentUser) 'The above returns a MAPIFolder, from which the Folders 'collection needs to be called to add a new folder set oNewFolder = oMAPIFolder.Folders.Add("New Tasks Folder",13) End Sub |
Item Method
By using the Item method with the Folders collection, you can access a specific folder in the collection either by index or by name. When accessing folders by name, remember that the names are case sensitive. The following example shows you how to access the Exchange Server Public Folder tree using the names of folders in the standard Public Folder hierarchy:
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oFolders = oNS.Folders set oPFFolderRoot = oFolders.Item("Public Folders") set oPFFolderChild = oPFFolderRoot.Folders.Item( _ "All Public Folders") oPFFolderChild.Display End Sub |
Remove Method
The Remove method deletes the specific object from the collection. The Remove method can take only the index as the identifier for the object you want to remove. Therefore, you must find the index of the folder you want to delete in the collection before calling the Remove method. The following code creates a new folder in your mailbox folders and then deletes it:
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") oCurrentUser = oNS.CurrentUser.Name set oMAPIFolder = oNS.Folders("Mailbox - " & oCurrentUser) set oMAPIFolders = oMAPIFolder.Folders set oNewFolder = oMAPIFolders.Add( _ "New Contacts Folder to Delete",10) oNewFolder.Display msgbox "This is the new folder." For i = 1 to oMAPIFolders.Count if oMAPIFolders.Item(i).Name = _ "New Contact Folder to Delete" then intFolderIndex = i end if next oMAPIFolder.Folders.Remove(intFolderIndex) msgbox "The folder has been deleted." End Sub |