GetOpenFolderName ? Does this exist?
GetOpenFolderName ? Does this exist?
(OP)
I was wondering if exist a method similar to GetOpenFilename that works for folder instead than for file
I would like to select a specific folder every time I run the code, in a similar way as I select a file with the GetOpenFilename (not opening the file, just retaining the path), instead than input the path within the VB code, that means every time i want to change the folder I have to write the code.
For example, in the code below:
fn = "C:\Foldername\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(fn)
Set fc = f.Files
etc. etc. etc
instead of having fn assigned by the code, i would like to select the path time by time selecting the folder from the window explorer.
Is this possible?
Should be, but I couldn't find the command !
Thanks
I would like to select a specific folder every time I run the code, in a similar way as I select a file with the GetOpenFilename (not opening the file, just retaining the path), instead than input the path within the VB code, that means every time i want to change the folder I have to write the code.
For example, in the code below:
fn = "C:\Foldername\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(fn)
Set fc = f.Files
etc. etc. etc
instead of having fn assigned by the code, i would like to select the path time by time selecting the folder from the window explorer.
Is this possible?
Should be, but I couldn't find the command !
Thanks





RE: GetOpenFolderName ? Does this exist?
There are a few functions that return a file name and path, or the FullName.
Those might get you whatever you need.
RE: GetOpenFolderName ? Does this exist?
In the following sample the function declarations and type declarations must be at the top of the code page in your VBA module. The function GetDirectory() can be used where needed in your main routine. It returns the directory name.
CODE
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Function GetDirectory(Optional MSG) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, X As Long, pos As Integer
' Root folder = Desktop
bInfo.pidlRoot = 0&
' Title in the dialog
If IsMissing(MSG) Then
bInfo.lpszTitle = "Select a folder."
Else: bInfo.lpszTitle = MSG
End If
' Type of directory to return
bInfo.ulFlags = &H1
' Display the dialog
X = SHBrowseForFolder(bInfo)
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal X, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else: GetDirectory = ""
End If
End Function
RE: GetOpenFolderName ? Does this exist?
GetDirectory = Left(path, pos - 1) & "\"