×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

GetOpenFolderName ? Does this exist?

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

RE: GetOpenFolderName ? Does this exist?

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.

RE: GetOpenFolderName ? Does this exist?

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

RE: GetOpenFolderName ? Does this exist?

(OP)
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) & "\"



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources