View Full Version : List foldersn outlook

10-20-2005, 06:23 AM
I am writing code to redirect mails from perticular EmailAddress.

For that i am giving user a form in which all the folders in outlook will be listed.

I am not getting the code to do this.

I can do this if i want to list folders from perticular folder like inbox by this :
Set myOlApp = CreateObject("Outlook.Application")
Set name = myOlApp.GetNamespace("MAPI")
Set fldFolder = name.Folders("olinbox")
'For Each prfolder In fldFolder

Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")

Call GetFolderInfo(fldFolder)

GetFolderInfo is listing folders in fldfolder.

But i want to check for all folders in outlook.(like folders in outbox, inbox....)

How can i list them?

10-20-2005, 10:55 AM
The Folders property of your NameSpace object contains the root of all your Outlook folders as a MAPIFolder named "Personal Folders." In turn, the Folders property of that MAPIFolder contains the MAPIFolder objects representing subfolders of Personal Folders, which includes Inbox, Outbox, Sent Items, Contacts, Journal, etc. The real trick is handling subfolders, especially nested ones. This is where having the procedure call itself can be quite handy.

Let's say your userform has a listbox named "listFolders". To identify each folder, we'll store the EntryID of the MAPIFolder in a bound, hidden (0"-width) column while we display the name in a second column. The following code would generate the list for you when you click on a button named CommandButton1:

Private Sub CommandButton1_Click()
Dim NS As NameSpace
Set NS = Application.GetNamespace("MAPI")
listSubFolders NS.Folders(1)
End Sub
Private Sub listSubFolders(f As MAPIFolder, Optional i As Integer)
Dim x As MAPIFolder
For Each x In f.Folders
' Optional test for email type folders only
' If x.DefaultItemType = olMailItem Then
listFolders.AddItem x.EntryID
listFolders.Column(1, listFolders.ListCount - 1) = String(i, "-") & x.Name
If x.Folders.count > 0 Then listSubFolders x, i + 1
' End If
End Sub

When the user selects a folder, you can programatically obtain the referenced MAPIFolder by calling NS.GetFolderFromID(listFolders) where NS is your NameSpace object.

10-20-2005, 10:02 PM
Giving error on :

listFolders.Column(1, listFolders.ListCount - 1) = String(i, "-") & x.Name

method or datamember not found...

10-21-2005, 05:02 AM
I can't seem to duplicate that error. It runs perfectly for me on Outlook 2000. What version of Outlook are you running?