Working with Outlook Folders

     

The NameSpace object stores all the Outlook folders, which means you can use it to return a reference to a folder and then work with that folder. Note that in the Outlook object model, folders are MAPIFolder objects.

Referencing Default Folders

One way to return a MAPIFolder object is to use the GetDefaultFolder method, which returns the default folder for a given type in the current profile. Here's the syntax:

  NameSpace  .GetDefaultFolder(  FolderType  ) 

NameSpace

The NameSpace object.

FolderType

A constant that specifies the type of folder. You can use any of the following defined constants: olFolderCalendar , olFolderContacts , olFolderDeletedItems , olFolderInbox , olFolderJournal , olFolderNotes , olFolderOutbox , olFolderSentMail , and olFolderTasks .

For example, if you want to work with the Inbox folder, your procedure would start with the following statements:

 Dim ns As NameSpace Dim ib As MAPIFolder Set ns = ThisOutlookSession.Session Set ib = ns.GetDefaultFolder(olFolderInbox) 

Using the Folders Property

Alternatively, you can use the NameSpace object's Folders property to return a Folders object that represents all of the MAPIFolder objects in the PST file. To reference a specific folder, use Folders( Index ) , where Index is one of the following:

  • An integer value with the first folder being 1, the second folder being 2, and so on.

  • The name of the folder in quotation marks.

The NameSpace object has only one folder ”known as the root ”which is usually called "Personal Folders." Therefore, the following statements are equivalent (assume ns is a NameSpace object):

 ns.Folders(1) ns.Folders("Personal Folders") 
graphics/note_icon.gif

For the Outlook procedures in this chapter, I've put everything into a text file named Chaptr11.txt , which you'll find on my Web site:

http://www.mcfedries.com/ABGVBA/Chapter11.txt

To use the code, create a module in Outlook's Visual Basic Editor, copy the code from Chapter11.txt , and then paste it into the module.


All the other mail folders are subfolders of this root. To get at them, you tack on another Folders property in the same way. For example, the first subfolder in the root is usually Deleted Items, so the following are equivalent:

 ns.Folders(1).Folders(1) ns.Folders("Personal Folders").Folders("Deleted Items") 

To help give you a feel for how these folders work, Listing 11.1 shows a procedure that runs through the first- and second-level folders in the namespace. Before you run this code, however, display the Visual Basic Editor's Immediate window by activating the View, Immediate Window command. (Check out Chapter 15, "Debugging VBA Procedures," for more information.)

Listing 11.1. A Procedure That Lists the First- and Second-Level Folders in the Outlook Namespace
 Sub ListFolders()     Dim ns As NameSpace     Dim folder As MAPIFolder     Dim subfolder As MAPIFolder     '     ' Set up the namespace     '     Set ns = ThisOutlookSession.Session     '     ' Run through the first-level folders     '     For Each folder In ns.Folders         Debug.Print folder.Name         '         ' Run through the second-level folders, if any         '         If folder.Folders.Count > 1 Then             For Each subfolder In folder.Folders                 Debug.Print "   " & subfolder.Name             Next 'subfolder         End If     Next 'folder     Set ns = Nothing End Sub 

After establishing the namespace session, the For Each...Next loop runs through the folders. The Debug.Print command is used to display the name of each folder (as given by the Name property) in the Immediate window, as shown in Figure 11.1. If the folder has subfolders, another For Each...Next loop runs through the subfolders in the same manner.

Figure 11.1. When you run the ListFolders procedure, the names of the email folders and subfolders are printed in the Immediate window.

graphics/11fig01.jpg

Prompting the User for a Folder

Another way to get a folder is to use the NameSpace object's PickFolder method:

  NameSpace  .PickFolder 

NameSpace The NameSpace object.

This method displays the Select Folder dialog box so that the user can choose a folder. The return value depends on the button the user clicks:

  • If the user clicks OK, the return value is a MAPIFolder object corresponding to the folder highlighted by the user.

  • If the user clicks Cancel, the return value is Nothing.

Listing 11.2 shows an example that invokes PickFolder and then tests the result.

Listing 11.2. A Procedure to Test the PickFolder Method
 Sub PickFolderTest()     Dim ns As NameSpace     Dim folder As MAPIFolder     '     ' Set up the namespace     '     Set ns = ThisOutlookSession.Session     '     ' Display the Select Folder dialog box     '     Set folder = ns.PickFolder     '     ' Test the return value     '     If Not folder Is Nothing Then         MsgBox "You picked " & folder.Name     End If End Sub 

Notice that the code uses the following test for the dialog box result:

 If Not folder Is Nothing Then 

If this returns True (that is, if the value of the folder variable is not equal to Nothing), then the name of the folder is displayed.

Some MAPIFolder Methods

Although you probably won't use them very often, the MAPIFolder object does come with a few methods:

MAPIFolder .CopyTo ” Copies the specified MAPIFolder to another folder:

 MAPIFolder.CopyTo(DestinationFolder) 

MAPIFolder

The MAPIFolder object you want to copy.

DestinationFolder

The MAPIFolder object to which you want the folder copied .

MAPIFolder .Delete ” Deletes the specified MAPIFolder .

MAPIFolder .MoveTo ” Moves the specified MAPIFolder to another folder:

 MAPIFolder.MoveTo(DestinationFolder) 

MAPIFolder

The MAPIFolder object you want to move.

DestinationFolder

The MAPIFolder object to which you want the folder moved.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net