How to select a folder using only common dialog control

Using VB6

The code.

CommonDialog1.DialogTitle = "Open File" CommonDialog1.Filter = "*.*" CommonDialog1.FilterIndex = 1 CommonDialog1.Flags = cdlOFNAllowMultiselect + cdlOFNExplorer CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly CommonDialog1.CancelError = True On Error Resume Next CommonDialog1.ShowOpen If Err Then 'MsgBox "Select Folder" Exit Sub End If 

From the above code, I select the file, but I do not want to select the file, I want to select only the folder. How to change the code.

Need help with vb6 code?

+7
vb6
source share
4 answers

It has been some time since I had to do some visual basic work, but I think, instead of using the general dialog box to get the file name to open, you should use the SHBrowseForFolder function which is already part of the Windows API. Here is a link to a page describing its use.

Update (2017): if the link is damaged, but the backup can be viewed on archive.org

+6
source share

To select a folder, you can use Shell Component and Automation.

 Private shlShell As Shell32.Shell Private shlFolder As Shell32.Folder Private Const BIF_RETURNONLYFSDIRS = &H1 Private Sub Command1_Click() If shlShell Is Nothing Then Set shlShell = New Shell32.Shell End If Set shlFolder = shlShell.BrowseForFolder(Me.hWnd, "Select a Directory", BIF_RETURNONLYFSDIRS) If Not shlFolder Is Nothing Then MsgBox shlFolder.Title End If End Sub 

You need to add a link to shell32.dll in your project. Use the Project / References ... menu and then select shell32.dll .

Or you can use the Windows API, as Twotymz suggests.

+6
source share

This is an old thread, but maybe it will help someone. This code works in VB6 for me:

 Private Sub ChooseDir_Click() Dim sTempDir As String On Error Resume Next sTempDir = CurDir 'Remember the current active directory CommonDialog1.DialogTitle = "Select a directory" 'titlebar CommonDialog1.InitDir = App.Path 'start dir, might be "C:\" or so also CommonDialog1.FileName = "Select a Directory" 'Something in filenamebox CommonDialog1.Flags = cdlOFNNoValidate + cdlOFNHideReadOnly CommonDialog1.Filter = "Directories|*.~#~" 'set files-filter to show dirs only CommonDialog1.CancelError = True 'allow escape key/cancel CommonDialog1.ShowSave 'show the dialog screen If Err <> 32755 Then ' User didn't chose Cancel. Me.SDir.Text = CurDir End If ChDir sTempDir 'restore path to what it was at entering End Sub 
+2
source share

I, however, is a more general VBA question by opening the folder selection dialog in VBA for Office> = 2k3.

I could not believe that it was so complicated, because I need the same functionality. It did a little googling. Here is a nice simple solution to watch

 Function GetFolderName() Dim lCount As Long GetFolderName = vbNullString With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = OpenAt .Show For lCount = 1 To .SelectedItems.Count GetFolderName = .SelectedItems(lCount) Next lCount End With End Function 
+1
source share

All Articles