tags:

views:

534

answers:

4

Using VB6

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 am selecting a file, But i don't want to select a file, I want to select only the folder. How to modify my code.

Need vb6 code Help?

A: 

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

  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

Or you can use the Windows API as Twotymz suggests.

Robert Harvey
+1  A: 

It's been a while since I've had to do any visual basic work but I think instead of using the common dialog box for getting the name of a file to open you should use the SHBrowseForFolder function which is already part of the Windows API. Here's a link to a page that describes it's usage.

Twotymz
A: 

Here's a class from the excellent vbAccelerator which lets you initialize the browse dialog to a specific folder (which is harder than you might think).

MarkJ
A: 

Ah, I though that is more general vba question anyway, opening select folder dialog in VBA for Office >=2k3.

I could not belive that it is so hard, as I need same functionality. Little googling made it. Here is nice simple solution take a look

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
Gadolin