More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 18-Dec-2018 14:18

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: Basic folder iteration
Description: Many common tasks in Outlook require you to iterate not just a single folder but that folder's subfolders, all the way down the folder hierarchy. This sample code provides a basic folder and item iteration framework. See notes below for usage and possible modifications.
Date: 10-Nov-2004  01:44
Code level: beginner
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 Sub ProcessFolder(StartFolder As Outlook.MAPIFolder)
    Dim objFolder As Outlook.MAPIFolder
    Dim objItem as Object
    On Error Resume Next
    
    ' do something specific with this folder
    Debug.Print StartFolder.FolderPath, StartFolder.Items.Count
    Debug.Print
        
    ' process all the subfolders of this folder
    For Each objFolder In StartFolder.Folders
        Call ProcessFolder(objFolder)
    Next
    
    ' process all the items in this folder
    For Each objItem In StartFolder.Items
        Call ProcessItem(objItem)
    Next
    
    Set objFolder = Nothing
End Sub
All 24comments
Page [ 1 2 3 Next >>  
  10-Nov-2004  09:49   
Notes:

1. StartFolder, the argument for the ProcessFolder procedure, can be any folder in your folder hierarchy. Use whatever technique is appropriate to your application to obtain that starting folder. If you want to quickly test this routine, you can use this code to prompt the user for a folder:

Sub ProcessAFolder()
    Dim objNS As Outlook.NameSpace
    Dim MyFolder As Outlook.MAPIFolder
    On Error Resume Next
    
    Set objNS = Application.GetNamespace("MAPI")
    Set MyFolder = objNS.PickFolder
    Call ProcessFolder(MyFolder)
    
    Set objNS = Nothing
    Set MyFolder = Nothing
End Sub

2. The Debug.Print statements are just a simple example of folder processing. You could do anything you want with the folder at that point. Note that FolderPath is a property available only in Outlook 2002 or later. For Outlook 2000, you might substitute Name for FolderPath or use the GetFolderPath() function, Listing 19.4 in my book, to return the folder path string.

3. ProcessItem would be a separate procedure doing whatever you might want to do with individual items -- remove attachments, etc. It is not included in the code above.
  08-Dec-2004  19:04   
Sue,

Is there anyway to specify the start folder as the "top" Outlook folder i.e, "Outlook Today -[User Name] rather than using "mapi.pickfolder"?

Thanks.

 
  08-Dec-2004  19:13   
As I said, you can use any folder as the starting point. The top level of the default store is the Parent folder of the Inbox, which you can return with the Namespace.GetDefaultFolder method:

Sub ProcessDefaultStore()
    Dim objNS As Outlook.NameSpace
    Dim MyFolder As Outlook.MAPIFolder
    On Error Resume Next
    
    Set objNS = Application.GetNamespace("MAPI")
    Set MyFolder = objNS.GetDefaultFolder(olFolderInbox )
    Call ProcessFolder(MyFolder.Parent)
    
    Set objNS = Nothing
    Set MyFolder = Nothing
End Sub
  09-Dec-2004  10:08   
Thanks Sue!
  20-Jan-2005  04:08   
Hi
Is there an easy way of creating a folder eg. My Folder\Client\Client1
So if Client does not exist it will create it as well as creating Client1

I have a process where i need create the path in exporer and simulate that in outlook, and then move my emails to that specific path, so i might be moving a email to folder that does not exist, and that i need to create on the fly

any ideas ?
thanks
  20-Jan-2005  04:12   
or how could i set my folder object say to the follwing directory
My Clients\Clients\Client1\Tuesday

?

thanks a stack

cheers
  20-Jan-2005  10:20   
Craig, to get a non-default Outlook folder, you need to walk the folder hierarchy using the Folders collections or use a function that does that for you. See http://www.outlookcode.com/d/code/getfolder.htm
 
  26-Oct-2005  15:01   
I need to determine if the current folder is Contacts or one of it's children. Can you reverse this script to read up? I stare at it, but don't see the answer.
  26-Oct-2005  15:35   
dannyboyo, it's even easier to work your way up the folder hierarchy, since each MAPIFolder has a Parent property that represents its Parent folder. You could use the Namespace.GetDefaultFolder method to get the EntryID of the default Contacts folder, then compare that ID with the ID for each Parent folder going up the hierarchy until you either get a match or run out of folders.

Listing 19.4 in my book is a GetFolderPath() function that shows how to walk up the folder hierarchy. You can download the code from the "get the code" link on this page.
  25-Jul-2006  04:04   
I've got the MyFolder.Parent OK but I have a further issue as I have another mail account on my PC at work. How would I navigate to the root of this account?
Page [ 1 2 3 Next >>