Microsoft KB Archive/174141

= How To Display Outlook Folder Names =

Article ID: 174141

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q174141



SUMMARY
The following code sample demonstrates how to print the names of all folders grouped under a specified Outlook folder. The sample assumes that the Microsoft Outlook mail client is installed.



MORE INFORMATION
The code below uses a recursive routine to iterate through a mail folder to produce a list of all its sub-folders in the Immediate Window.

Step-by-Step Example
 Install Microsoft Outlook if it is not already installed. Open a Standard EXE project in Microsoft Visual Basic. Add a reference to the Microsoft Outlook 8.0 Object Library (msoutl8.olb) using the Project item on the References menu. Set the Startup Object to "Sub Main" from the Project Properties dialog. Add a Standard Module (.BAS) file to the project.  Insert the following code into the module: (Modify the FOLDER_TO_OPEN constant in the code below as appropriate and Execute.) Option Explicit

Private Sub Main Dim olMAPI As Outlook.NameSpace Dim Folder As Outlook.MAPIFolder '// Modify as appropriate. Const FOLDER_TO_OPEN = "Mailbox - John Doe"

Set olMAPI = GetObject("",  "Outlook.application").GetNamespace("MAPI") Call PrintFolderNames(olMAPI.Folders(FOLDER_TO_OPEN), "-  >") Set olMAPI = Nothing End Sub

Sub PrintFolderNames(tempfolder As Outlook.MAPIFolder, a$) Dim i As Integer If tempfolder.Folders.Count Then Debug.Print a$ & " " & tempfolder.Name For i = 1 To tempfolder.Folders.Count Call PrintFolderNames(tempfolder.Folders(i), a$ & "-  >") Next i       Else Debug.Print a$ & " " & tempfolder.Name End If     End Sub  Run the project.</li></ol>

NOTE: The sample code in this article works only when the FOLDER_TO_OPEN is set to a folder you can open, such as your own folder or the public folder. Otherwise a runtime error is generated.

Additional query words: kbNoKeyword

Keywords: kbhowto KB174141

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.