Outlook 2000 VBA Programmers Reference
The Add method will create and return a new MAPIFolder object in the current Folders collection.
Set MAPIFolderObject = FoldersCollection.Add(Name [, Type])
|Name||String||Required, the name of the folder that you see in Outlook.|
|Type||Long||Optional, can be one of the OlDefaultFolder constants. If no type is supplied then the new MAPIFolder will inherit its Type from the parent MAPIFolder.|
Valid OlDefaultFolder constants for this method are as follows:
|olFolderCalendar||9||Creates a new Calendar folder|
|olFolderContacts||10||Creates a new Contacts folder|
|olFolderDrafts||16||Creates a new Drafts folder|
|olFolderInbox||6||Creates a new Inbox folder|
|olFolderJournal||11||Creates a new Journal folder|
|olFolderNotes||12||Creates a new Notes folder|
|olFolderTasks||13||Creates a new Tasks folder.|
The following constants are not valid when trying to create a new folder:
|olFolderDeletedItems||3||Represents a DeletedItems folder|
|olFolderOutbox||4||Represents a Outbox folder|
|olFolderSentMail||5||Represents a SentMail folder|
Dim onMAPI As NameSpace Dim ofChosenFolder As MAPIFolder Set onMAPI = Application.GetNamespace("MAPI") Set ofChosenFolder = onMAPI.PickFolder ofChosenFolder.Folders.Add "Inbox 2", olFolderInbox
In the above example, the user is prompted by the Pick Folder dialog to choose one of their folders. A new folder called "Inbox 2" is then created in the chosen. The figure below shows the newly created "Inbox 2" MAPIFolder in the original Inbox MAPIFolder.
More details on the PickFolder method of the NameSpace object are given in chapter 6.GetFirst Method
The GetFirst method allows you to navigate to the first MAPIFolder object in the referenced collection. If there is no MAPIfolder object in the collection then Nothing is returned.
Set MAPIFolderObject = FoldersCollection.GetFirst
It is always wise to first get a reference to the Folders collection before you use the Get methods on the collection.
Dim onMAPI As NameSpace Dim ofRootFolder As Folders Dim ofFirstFolder as MAPIFolder Set onMAPI = Application.GetNamespace("MAPI") Set ofRootFolder = onMAPI.Folders Set ofFirstFolder = ofRootFolder.GetFirst MsgBox ofFirstFolder
This simple example first sets a reference to the root folders collection. It then uses the GetFirst method to get the first folder within this collection, having first declared it to be a MAPIFolder. Finally its name is displayed in a message box.
The GetLast method works in a similar fashion to the GetFirst method except it will return the last MAPIFolder in the collection. If there are no folders in the collection it will return Nothing.
Set MAPIFolder = FoldersCollection.GetLast
The GetNext method moves to the next MAPIFolder in the collection.
Set MAPIFolderObject = FoldersCollection.GetNext
Dim ofcPersonalFolders As Folders Dim ofFolder As MAPIFolder Set ofcPersonalFolders = Application.GetNamespace("MAPI").Folders.GetFirst.Folders Set ofFolder = ofcPersonalFolders.GetFirst Do Until ofFolder Is Nothing MsgBox ofFolder.Name Set ofFolder = ofcPersonalFolders.GetNext Loop
In the example above we walk through the folders of the first Folders collection within the Root folders collection for the current profile. For my profile the first collection within the root folder collection is my Personal Folder. A reference is first set to this folder and then the GetFirst and GetNext methods are used to move through each of the folders in this collection. A Do Until loop is employed to check when the end of the Folders collection is reached and the name of each folder is displayed in a message box.
The GetPrevious method provides a way to move to a previous MAPIFolder object in the current collection
Set MAPIFolderObject = FoldersCollection.GetPrevious
Again, I hate to repeat myself but it is always wise to get a reference to the collection that you want to work with before you use any of the Get methods of the Folders collection.
The Item method provides a way to reference a particular MAPIFolder. This can be achieved by using the index number of the MAPIFolder within the collection or by using the name of the MAPIfolder.
Set MAPIFolderObject = FoldersCollection.Item(Index)
|Index||Variant||Required, either a long representing the position of the MAPIFolder object within the collection or its name as a string.|
Dim onMAPI As NameSpace Dim ofInbox As MAPIFolder Dim ofFolder As MAPIFolder Set onMAPI = Application.GetNamespace("MAPI") Set ofInbox = onMAPI.Folders.GetFirst.Folders.Item("Inbox") Set ofFolder = onMAPI.Folders.GetFirst.Folders(2)
In this example we get references to two different folders. For the first variable, ofInbox, we pick the folder by its name. The second variable, ofFolder, is set to the second folder within the first folder in the root folders using the index of the folder. For my personal setup this gives me a reference to the Contacts folder, although this may differ for you. Because Item is the default method it is not necessary to type it explicitly, as shown in the last line of code.
The Remove method will take the MAPIFolder referenced by the index parameter and permanently remove it from the Folders collection.
|Index||Long||Required, represents the position of the MAPIFolder object within the collection that you wish to remove.|
Be aware that when a folder is removed with this method, it will remove ALL folders and items within that folder without providing a warning.
Dim onMAPI As NameSpace Dim ofFolder As MAPIFolder Dim iFor As Integer Set onMAPI = Application.GetNamespace("MAPI") Set ofFolder = onMAPI.GetDefaultFolder(olFolderInbox) ofFolder.Folders.Add "Inbox 3", olFolderInbox For iFor = 1 To ofFolder.Folders.count If ofFolder.Folders.Item(iFor).Name = "Inbox 3" Then ofFolder.Folders.Remove iFor Exit For End If Next
In this example we first add a MAPIFolder to the ofFolder object. We then walk through the Folders collection to locate the MAPIFolder. Once we do locate it we use the Remove method to delete it from the collection and exit the For Loop.
Page 2 of 6