VBA to find path of User created folder

T

The Writings

Hi There,

Is there a VBA code that I can use to find a user created folder?
A bit like serach in Windows exporer.

Kind regards
TW
 
M

Michael Bednarek

Is there a VBA code that I can use to find a user created folder?
A bit like serach in Windows exporer.

If you know the exact path, you can address it with
Application.GetNamespace("MAPI").Folders("Level 1").Folders("Level 2").Folders("Level 3")
e.g.
Application.GetNamespace("MAPI").Folders("Mailbox - John Doe").Folders("Inbox").Folders("foo")

If you only know the folder name but not the path, you have to
traverse the folder tree to find it. Here is some code that will
traverse recursively a folder tree and list folder names.

Sub ListFolders(fldFolder As Variant)
Dim varFolder As Variant
Static lngLevel As Long

lngLevel = lngLevel + 1
For Each varFolder In fldFolder.Folders
Debug.Print String(lngLevel, "+") & varFolder.Name
If varFolder.Folders.Count > 0 Then
Call ListFolders(varFolder)
End If
Next
lngLevel = lngLevel - 1
End Sub

Sub testListFolders()
Call ListFolders(ActiveExplorer.CurrentFolder)
End Sub

Comparing the folder names with a search string and building a
complete path is left as an exercise for the reader. Good luck.
 
T

The Writings

If you know the exact path, you can address it with
  Application.GetNamespace("MAPI").Folders("Level 1").Folders("Level 2").Folders("Level 3")
e.g.
  Application.GetNamespace("MAPI").Folders("Mailbox - John Doe").Folders("Inbox").Folders("foo")

If you only know the folder name but not the path, you have to
traverse the folder tree to find it. Here is some code that will
traverse recursively a folder tree and list folder names.

  Sub ListFolders(fldFolder As Variant)
  Dim varFolder As Variant
  Static lngLevel As Long

  lngLevel = lngLevel + 1
  For Each varFolder In fldFolder.Folders
    Debug.Print String(lngLevel, "+") & varFolder.Name
    If varFolder.Folders.Count > 0 Then
      Call ListFolders(varFolder)
    End If
  Next
  lngLevel = lngLevel - 1
  End Sub

  Sub testListFolders()
  Call ListFolders(ActiveExplorer.CurrentFolder)
  End Sub

Comparing the folder names with a search string and building a
complete path is left as an exercise for the reader. Good luck.

Thank You Michael for the coding.
I tried to run this but I do not see any output?

TW
 
M

Michael Bednarek

Is there a VBA code that I can use to find a user created folder?
A bit like serach in Windows exporer.

If you know the exact path, you can address it with
  Application.GetNamespace("MAPI").Folders("Level 1").Folders("Level 2").Folders("Level 3")
e.g.
  Application.GetNamespace("MAPI").Folders("Mailbox - John Doe").Folders("Inbox").Folders("foo")

If you only know the folder name but not the path, you have to
traverse the folder tree to find it. Here is some code that will
traverse recursively a folder tree and list folder names. [snip]
    Debug.Print String(lngLevel, "+") & varFolder.Name
[snip]
Thank You Michael for the coding.
I tried to run this but I do not see any output?

The output of Debug.Print is shown in the VBA Editor's (Alt+F11)
Immediate Window (Ctrl+G).
 
T

The Writings

Is there a VBA code that I can use to find a user created folder?
A bit like serach in Windows exporer.
If you know the exact path, you can address it with
  Application.GetNamespace("MAPI").Folders("Level 1").Folders("Level2").Folders("Level 3")
e.g.
  Application.GetNamespace("MAPI").Folders("Mailbox - John Doe").Folders("Inbox").Folders("foo")
If you only know the folder name but not the path, you have to
traverse the folder tree to find it. Here is some code that will
traverse recursively a folder tree and list folder names. [snip]
    Debug.Print String(lngLevel, "+") & varFolder.Name
[snip]
Thank You Michael for the coding.
I tried to run this but I do not see any output?

The output of Debug.Print is shown in the VBA Editor's (Alt+F11)
Immediate Window (Ctrl+G).

Thanks Michael
 

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