• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Open File CmdDialog

Hi all,
How to open the Open File Window for user to choose a specific FOLDER (not file) ?

coz the macro tat i am doing need user to specific a target folder to work on, and i am going to put a 'BROWSE' command button for that purpose
After user has select the target path, the path will be shown in a textbox as well.

Can anyone tell me how to do that? And what library to be included ?

0
KCChan
Asked:
KCChan
  • 2
1 Solution
 
SteinerCommented:
Put this into a module:

Option Explicit

Public Type BrowseInfo
   hwndOwner As Long
   pIDLRoot As Long
   pszDisplayName As Long
   lpszTitle As Long
   ulFlags As Long
   lpfnCallback As Long
   lParam As Long
   iImage As Long
End Type

Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lsString1 As String, ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

Public Const BIF_RETURNONLYFSDIRS = 1
Public Const MAX_PATH = 256

Public Function BrowseForFolder(Beschriftung As String)
Dim pidl&, path$, bi As BrowseInfo
   With bi
      .hwndOwner = Screen.ActiveForm.hWnd
      .lpszTitle = lstrcat(Beschriftung, " ")
      .ulFlags = BIF_RETURNONLYFSDIRS
   End With
   pidl = SHBrowseForFolder(bi)
   If pidl Then
      path = String(MAX_PATH, 0)
      SHGetPathFromIDList pidl, path
      CoTaskMemFree pidl
      path = Left$(path, InStr(path, vbNullChar) - 1)
   End If
   BrowseForFolder = path
End Function

In your form you just have to add a button with the following code:

Private Sub Command1_Click()
   Text1.Text = BrowseForFolder("Choose a directory:")
End Sub

Greets
Steiner
0
 
KCChanAuthor Commented:
Thanks a lot, this is what exactly i was looking for.

0
 
criCommented:
And you got it twice....

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now