How to modify the code so it checks the subfolders as well

Posted on 2010-09-01
Last Modified: 2012-06-22
I found the following code on the net. I ran this and I realize that it is looking to the folders under mailbox. But not looking to the subfolders like "Inbox". I want to be able to check subfolders too, specifically the ones under Inbox. How would I modify this code so it will look for the subfolders as well.

Dim myolApp As Outlook.Application

    Dim myNameSpace As Outlook.NameSpace

    Dim myFolder As Outlook.MAPIFolder

    Dim found As Boolean

    Dim strFolderName As String


    'specify the folder you are looking for

    strFolderName = "Inbox"


    found = False

    Set myolApp = GetObject(, "Outlook.Application")

    Set myNameSpace = myolApp.GetNamespace("MAPI")

    For i = 1 To (myNameSpace.Folders.Count)

        For j = 1 To (myNameSpace.Folders(i).Folders.Count)


'         MsgBox myNameSpace.Folders(i).Folders(j).Name

            If myNameSpace.Folders(i).Folders(j) = strFolderName Then



                Set Application.ActiveExplorer.CurrentFolder = _


                Exit For

            End If


        If found = True Then Exit For


    If found = True Then MsgBox "Rune Lange Sen Bir ibnesin"



    Set myolApp = Nothing

    Set myNameSpace = Nothing

Open in new window

Question by:awesomejohn19
LVL 17

Expert Comment

ID: 33580249
The following link has code to find all the folder in a mailbox:

Accepted Solution

PJBX earned 500 total points
ID: 33709509
Declare mysubfolder varialbe and add it within the loop of the main folders.

Dim mysubfolder As Outlook.MAPIFolder
For Each mysubfolder In myfolder.Folders  

Next mysubfolder  

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Read this checklist to learn more about the 15 things you should never include in an email signature.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now