The NameSpace object has a Folders collection that has all your
folders in it. Each item in the top level Folders collection is an
InfoStore such as a PST file. Under that is another Folders collection
that has each of your top level folders such as Inbox and Calendar. If
any of them have subfolders they will be in the Folders collection of
that folder (Inbox, etc.).
Dim oOL As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim oFolder As Outlook.MAPIFolder
Set oOL = CreateObject("Outlook.Application")
Set oNS = oOL.GetNameSpace("MAPI")
Set colFolders = oNS.Folders(1) 'assuming only 1 PST open
For Each oFolder In colFolders
Debug.Print oFolder.Name
If oFolder.Folders.Count > 0 Then
MsgBox "This folder has subfolders"
'can access them the same way
End If
Next
Here's a sample of some code I've used to set the default form for all
Contacts folders in an InfoStore. It first gets the default Contacts
folder, then its parent folder which is the TopOfInformationStore
(shown in the UI as Mailbox - xxx or Personal Folders). You can modify
this code to recursively iterate the entire collection of folders.
Public Sub SetForm()
Dim oFolder As Outlook.MAPIFolder
Dim oParent As Outlook.MAPIFolder
On Error Resume Next
blnPublished = False
Set oFolder = g_objNS.GetDefaultFolder(olFolderContacts)
Set oParent = oFolder.Parent
Call SetAllContactFolders(oParent)
Set oFolder = Nothing
Set oParent = Nothing
End Sub
Private Sub SetAllContactFolders(oSourceFolder As Outlook.MAPIFolder)
Dim oFolder As Outlook.MAPIFolder
Dim colFolders As Outlook.Folders
On Error Resume Next
Set colFolders = oSourceFolder.Folders
If Not (colFolders Is Nothing) Then
For Each oFolder In colFolders
If oFolder.DefaultItemType = olContactItem Then
'do whatever
End If
'recursive call to SetAllContactFolders.
'repeat until no more subfolders
Call SetAllContactFolders(oFolder)
Next
End If
Set oFolder = Nothing
Set colFolders = Nothing
End Sub
Here's a sample of some code I've used to set the default form for all
Contacts folders in an InfoStore. It first gets the default Contacts
folder, then its parent folder which is the TopOfInformationStore
(shown in the UI as Mailbox - xxx or Personal Folders). You can modify
this code to recursively iterate the entire collection of folders.
Public Sub SetForm()
Dim oFolder As Outlook.MAPIFolder
Dim oParent As Outlook.MAPIFolder
On Error Resume Next
blnPublished = False
Set oFolder = g_objNS.GetDefaultFolder(olFolderContacts)
Set oParent = oFolder.Parent
Call SetAllContactFolders(oParent)
Set oFolder = Nothing
Set oParent = Nothing
End Sub
Private Sub SetAllContactFolders(oSourceFolder As Outlook.MAPIFolder)
Dim oFolder As Outlook.MAPIFolder
Dim colFolders As Outlook.Folders
On Error Resume Next
Set colFolders = oSourceFolder.Folders
If Not (colFolders Is Nothing) Then
For Each oFolder In colFolders
If oFolder.DefaultItemType = olContactItem Then
'do whatever
End If
'recursive call to SetAllContactFolders.
'repeat until no more subfolders
Call SetAllContactFolders(oFolder)
Next
End If
Set oFolder = Nothing
Set colFolders = Nothing
End Sub
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.