Question

First of all, I must say that I'm running a code in a machine that has very few references installed and no Office at all.

I need to open an "explorer.exe" instance with the Shell command, browse through folders, enter in a selected (or newly created) one and finally click on "Ok" on a MsgBox (which pops up simultaneously) to close the Shell and return the selected folder path to a variable.

I have no idea how to achieve that. When I use CurDir, I end up getting a folder which is not the selected one.

Code used (which didn't work):

Sub BrowseForFolder()

ActualDir = "D:\"
Call Shell("C:\Windows\explorer", ActualDir, 1)

If Msgbox("Browse into folder or create a new one and then browse into it, then click ok", vbOkOnly, "Browse") = vbOk Then
    ' here should be the command to return the path. The following doesn't work since it returns always "D:\"
    ActualDir = CurDir
End If

' Here I have to close the Shell - I have no idea what to write to do it

End Sub
Was it helpful?

Solution

Below the code suggested by LS_dev which worked totally:

Private Sub SelectFolder_Click()

Dim objShell As Shell       

Had to add the shell references

Dim ssfWINDOWS As Long
Dim objFolder As Folder2    

for some reason "Folder" doesn't work

ssfWINDOWS = 36
Set objShell = New Shell
Set objFolder = objShell.BrowseForFolder(0, "Select Folder", 0, "D:\")

If (Not objFolder Is Nothing) Then
    FolderName = objFolder.Self.path
End If

Set objShell = Nothing

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top