PDA

View Full Version : FILE DIALOG BOX



jperrotta
2004-06-14, 04:10 PM
I AM TRYING TO FIGURE OUT IN VBA HOW TO DISPLAY A FILE DIALOG BOX AND BE ABLE TO SELECT MULTIPLE FILES FROM A DIRECTORY AND CREATE A LIST OF THE SELECTED FILES.

ANY IDEAS?

Ed Jobe
2004-06-14, 04:31 PM
I AM TRYING TO FIGURE OUT IN VBA HOW TO DISPLAY A FILE DIALOG BOX AND BE ABLE TO SELECT MULTIPLE FILES FROM A DIRECTORY AND CREATE A LIST OF THE SELECTED FILES.

ANY IDEAS?
First of all, please remember your email etiquette. All caps is regarded as yelling.

There are a couple of ways to call the lisp (filedia) function, but I prefer to use the windows api to call windows dialogs. A couple of people have created cls files that encapsulate the functions you need. I have taken some of these and added to them. I'll attach here. After you get the filenames, save them to a collection. Then use that to populate a combobox, etc.

jason907238
2007-08-02, 02:36 PM
So I take it that there is easy way to use something like FileBrowserDialog?

I downloaded the filedialogs[1].zip file. I cut and pasted the text in a to a Class Module. Some if the lines are showing up in red....

VERSION 1.0 CLASS

Attribute VB_Name = "FileDialogs"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True

I was just trying to add a file browser to one of my routines. The problem is that I don't really have time at the moment so spend adding this. If there is nothing quick, I will just have to cut and paste the path which I need from a Explorer window.

Christopher.cornell
2007-08-02, 03:09 PM
Could you use an open file dialog box or do you actually want it to be on the form. Because you can select multiple files with a common dialog box using the below (some of which I found from other places) The stuff I found I modified a little bit but there is the gyst of it. The different flags you have will allot for different things. The "cdlOFNAllowMultiselect" will allow multiselect and if you want it to look nice add this flag too "cdlOFNExplorer"

'Initial Directory
CommonDialog1.InitDir = path
' Set flags
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNPathMustExist + cdlOFNFileMustExist + cdlOFNAllowMultiselect + cdlOFNExplorer
' Set filters
CommonDialog1.Filter = "All Files (*.*)|*.*|DWG (*.dwg)|*.dwg"

' Display the Save dialog box
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen

'''''''''''''''''''''''''
'This code snippet was found at
'http://216.239.51.104/search?q=cache:gZ672ASbZfkJ:vb-helper.com/howto_select_multiple_files.html+common+dialog+selecting+multiple+files&hl=en&ct=clnk&cd=1&gl=us 'There was no name on it.

Dim entries As Variant
Dim dir_name As String
Dim i As Integer

entries = Split(CommonDialog1.FileName, vbNullChar)

' See if there is more than one file.
If UBound(entries, 1) = LBound(entries, 1) Then
' There is only one file name.
ListBox1.AddItem entries(LBound(entries, 1))
Else
' Get the directory name.
dir_name = entries(LBound(entries, 1))
If Right$(dir_name, 1) <> "\" Then dir_name = _
dir_name & "\"

' Get the file names.
For i = LBound(entries, 1) + 1 To UBound(entries, 1)
ListBox1.AddItem dir_name & entries(i)
Next i
End If

If i = 0 Then
Label23.Caption = dir_name & entries(i)
Else
Label23.Caption = dir_name & entries(i - 1)
End If
Exit Sub



sorry if its all in here. I dont know how to do the code thing

jason907238
2007-08-02, 04:07 PM
First off, I am using VBA and I have not found a file dialog box to use. I have searched the web but I have not found anything yet. I don't know what Reference to use if there is one. At the moment, I am just trying to get a directory name and path. But in the future, I would use it to get a file name and path.

Microsoft must has something easy out there instead of making it from scratch since most programs use the same or similar browser.

Ed Jobe
2007-08-02, 06:46 PM
I downloaded the filedialogs[1].zip file. I cut and pasted the text in a to a Class Module. Some if the lines are showing up in red....
The file in the zip already is a class file (*.cls). The text you see as red are attributes. Just import it and you shouldn't have any problems. Rt+click in the project browser and select "Import File..."

p.s. the file browser dialog is different than the folder browser.

Christopher.cornell
2007-08-02, 06:48 PM
Sorry I forgot to put that its under tools/references and its called microsoft common dialog control. Something along those lines. You can use this to get a path just have it return the name and chop off the file name. Like I said I didnt know if it was exactly what was needed.


EDIT: Yeah I figured that, but it was an option that I thought of.

Ed Jobe
2007-08-02, 07:53 PM
Microsoft must has something easy out there instead of making it from scratch since most programs use the same or similar browser.
The Common dialog control previously mentioned comes with VB6, not vba. Its best to use the Windows api, which is what FileDialogs.cls does. .NET has stuff built in though.

Christopher.cornell
2007-08-03, 12:04 PM
It seems to work just fine for me. I am using VBA. But I dont know much about all the differences of and of the flavors of vb. I have never touched the stuff before this summer.

Ed Jobe
2007-08-03, 02:27 PM
If the control gets installed on your pc, which apparently it hasn't for Jason, you can use it, but its not legal in vba according to the VB EULA.

Christopher.cornell
2007-08-03, 02:59 PM
Ahh.. Ok that makes sense. It probably is using the one from VS2005. Which would be my guess. Do you know why its not legal? Just curious

lance.81922
2007-08-12, 12:35 AM
The CommonDialog control ships only with VB, and not with VBA. As for the reason that it does NOT ship with VBA, Microsoft only knows. I guess you could ask them -- for the price of a tech support call.

mikeosborne
2009-11-25, 01:54 PM
Can some help me, I have read through this post and the six other that come up when I search for file dialog boxes and gone through the code Ed attached to this post and I can't make this do what I want it to do. I would like to be able to select multiple files from the open dialog window and then populate a list box with those file names. Can I do that with this code?

Ed Jobe
2009-11-25, 03:28 PM
Can some help me, I have read through this post and the six other that come up when I search for file dialog boxes and gone through the code Ed attached to this post and I can't make this do what I want it to do. I would like to be able to select multiple files from the open dialog window and then populate a list box with those file names. Can I do that with this code?
This should give you what you need to know. Notice that you have to set the multiselect property. If you want to know more on how this functions, go to msdn and look up the api functions used by FileDialogs.


Public Sub OpenFile()
'sample to show how to use FileDialogs
Dim objFile As FileDialogs
Dim strFilter As String
Dim strFileName As String
Dim colFileNames As New Collection
Dim colFileTitles As New Collection

Set objFile = toolbox.CreateFileDialogsClass
'desc,filter combinations must all be separated with pipe char "|"
strFilter = "All Files (*.*)|*.*|Drawings (*.dwg)|*.dwg"
objFile.Title = "Open a drawing"
'default dir is CurDir
objFile.StartInDir = "J:\ea\drawings\substatn\wal"
objFile.Filter = strFilter
objFile.MultiSelect = True
'return a valid filename
strFileName = objFile.ShowOpen
If Not strFileName = vbNullString Then
'use this space to perform operation

Dim sTemp() As String, nCount As Integer
sTemp = Split(strFileName, vbNullChar)
' If array contains no elements,
' UBound returns -1.
If UBound(sTemp) > LBound(sTemp) Then
' We have more than one array element!
' Remove backslash if sPath is the root folder.
' If Len(sPath) = 3 Then _
' sPath = Left$(sPath, 2)
' Loop through the array, and create the
' collections; skip the first element
' (containing the path name), so start the
' counter at 1, not at 0.
For nCount = 1 To UBound(sTemp)
colFileTitles.Add sTemp(nCount)
' If the string already contains a backslash,
' the user must have selected a shortcut
' file, so we don't add the path.
colFileNames.Add IIf(InStr(sTemp(nCount), _
"\"), sTemp(nCount), _
objFile.StartInDir & "\" & sTemp(nCount))
Next
' Clear this variable.
' strFileName = vbNullString
End If
Replace strFileName, vbNullChar, vbCrLf, , -1
MsgBox strFileName
End If
Set objFile = Nothing


End Sub