VBA to find path of User created folder

  • Thread starter Thread starter The Writings
  • Start date Start date
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
 
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.
 
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
 
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).
 
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
 
Back
Top