Loop through all folders in a mailbox


G

Guest

I'm trying to loop through all folders in a mailbox with no luck. I'm using
GetLast which seems to get me to my mailbox (can I always assume this?). This
is what I have so far, any help would be appreciated.

Sub LoopFolders()
Dim myOlApp As Application
Dim myNameSpace As NameSpace
Dim myFolder As MAPIFolder

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")

Set myFolder = myNameSpace.Folders.GetLast

While Not myFolder Is Nothing
MsgBox myFolder
Set myFolder = myFolder.Folders.GetNext '<-GetPrevious does not seem to
work
Wend
End Sub
 
Ad

Advertisements

S

Sue Mosher [MVP-Outlook]

The recursive procedure at http://www.outlookcode.com/codedetail.aspx?id=628 shows how to iterate all folders beneath a given starting folder. For a mailbox, you'd use Namespace.GetDefaultFolder to return the Inbox, then use the Parent of the Inbox as the starting folder.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Thanks for the reply Sue,

I tried the previous folder statement but I get an error code 91. Probably a
syntax error. Can you look at the last line and tell me what is should be.

Sub getFolders()
Dim myOlApp As Application
Dim myNameSpace As NameSpace
Dim myFolder As MAPIFolder
Dim myInbox As MAPIFolder

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")

Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set MyRoot = myNameSpace.GetDefaultFolder(olFolderInbox)

MsgBox myInbox.Folders.GetPrevious
End Sub
 
S

Sue Mosher [MVP-Outlook]

If it returns Nothing, then it means there is no previous folder, which is expected since you haven't used GetNext or GetLast.

Is a particular reason why you didn't try the technique I suggested?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Ad

Advertisements

G

Guest

Sure, there is a definate reason why I didn't use your technique... I didn't
read your response properly. I thought you said "Previous" not "Parent". Lost
in translation from my outlook to my Visual Basic Editor (new I should have
got dual monitors). Sorry, I'm a little new to this VBA programming stuff and
I'm unfamiliar with the syntax.

Thanks for your help (embarrised),
 

Ask a Question

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.

Ask a Question

Top