BTW, I copied and pasted some of the code for the example but made changes to it.
Even though one procedure is named RecursiveSearchFolders it is not recursive. I was too lazy to change its name.
It didn't appear you were concerned with subfolders.
I also didn't add error handling for instance if they enter a 7 and there are only 6 folders.
edit: Now that I'm looking at it again I see a few changes that can be made and still accomplish the same thing thing with a little less code.
heres the changes:
Code:
Option Compare Database
Option Explicit
Dim dict As New Scripting.Dictionary
Private Sub Command2_Click()
Me.Text0 = FSBrowse(, msoFileDialogFolderPicker) 'get the top folder
End Sub
Private Sub Command3_Click()
Dim strMsg As String
Dim x As Variant
If Nz(Me.Text0, "") = "" Then Exit Sub
Call RecursiveSearchFolders(Me.Text0, strMsg) 'populate the dictionary and get the strMsg (strMsg passed ByRef)
x = InputBox(strMsg, "Select a Folder Number")
If x > dict.Count Then Exit Sub 'if number entered is higher than folder count exit sub
If Nz(x, "") <> "" Then
Application.FollowHyperlink dict.Item(x)
End If
End Sub
Sub RecursiveSearchFolders(fld As String, ByRef strMsg As String)
dict.RemoveAll 'clear dictionary
Dim fold As Folder
Dim fldr As Folder
Dim i As Integer
i = 1
Dim fso As New FileSystemObject
Set fldr = fso.GetFolder(fld)
For Each fold In fldr.SubFolders
dict.Add CStr(i), CStr(fold.Path) 'add number (i) as index and Folder path as item to dictionary
strMsg = strMsg & i & Chr(9) & fold.Name & vbNewLine 'add number and folder name to strMsg
i = i + 1
Next
End Sub
Also note you need a reference to MS scripting runtime and MS office XX object library.