Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetOpenFolderName ? Does this exist? 1

Status
Not open for further replies.

iuston

Mechanical
Jan 26, 2005
12
0
0
IT
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
 
Replies continue below

Recommended for you

Well, there really is no such thing as an "open" folder.

There are a few functions that return a file name and path, or the FullName.

Those might get you whatever you need.
 
You can write your own routine to get a folder using API calls.
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:
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
    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
 
Thanks cumming54, that is what I was looking for , I only added & "\" in the GetDirectory to have it working when adding the file name.
GetDirectory = Left(path, pos - 1) & "\"

220ot7.gif
 
Status
Not open for further replies.
Back
Top