Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Mar 3, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Common Dialog - folder selection?

In Word 7.0, under Tools, Options, File Locations tab, there is a dialog that comes up to define the folder containing certain types of files. This dialog allows folder selection only, not file selection. Is this acessible through VB somehow? Is it a common dialog that I can get to? If not, is there a simple method to create this type of dialog?

The standard Common Dialog is unable to create this dialog. However, an API call to the shell is able to do it. Add the code to your project, and then call the routine like so:

Dim sPath as String

sPath = fnBrowseForFolder(Me.hWnd, "Select Output Directory")
Me refers to a form, so you can specify a particular form if you wish. The dialog will approximately center itself within the borders of the form, so you may want to pick your MDI frame if you are using one.
Option Explicit

Private 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

Private Const MAX_PATH = 260

Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
       (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" _
       (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" _
       (ByVal pidList As Long, ByVal lpBuffer As String) As Long

' fnBrowseForFolder
' This function will create the directory browser
' window common to other applications.
Public Function fnBrowseForFolder(hWndOwner As Long, sPrompt As String) As

   Dim iNull As Integer
   Dim lpIDList As Long
   Dim lResult As Long
   Dim sPath As String
   Dim udtBI As BrowseInfo
   Dim iPos As Integer
   With udtBI
       .hWndOwner = hWndOwner
       .lpszTitle = lstrcat(sPrompt, "")
   End With

   lpIDList = SHBrowseForFolder(udtBI)
   sPath = Space$(512)
   If lpIDList Then
      lResult = SHGetPathFromIDList(lpIDList, sPath)
      If lResult Then
         iPos = InStr(sPath, Chr$(0))
         sPath = Left$(sPath, iPos)
         sPath = ""
      End If
   End If
   fnBrowseForFolder = sPath

End Function
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date