Good Morning guys,

Can someone please tell me how to insert a fileopen dialog in VB? What I want is when the user clicks on a button, the program takes them to a particular directory or folder, that way they will be able to select files on their own from that folder.

For some reason, I do not have the CommonDialog control which should be under "Additional Controls." Is there a way I can install the commondialog control into my system? Please let me know.

If there is a thread that already exist in this forum regarding opening the dialog box, please send it to me. Thank you very much for your anticiapted cooperation.


Recommended Answers

All 12 Replies

in a module:

' /* Code Taken From: */
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

public Function SelectFileOpenDialog()
Dim strTemp, strTemp1, pathStr As String
Dim i, n, j As Long
Dim lReturn As Long
Dim sFilter As String
Dim Fname As String

OpenFile.lStructSize = Len(OpenFile)
sFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Select File"
OpenFile.flags = 0

lReturn = GetOpenFileName(OpenFile)

If lReturn = 0 Then
MsgBox "You didn't select any file"
'MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)

Fname = Trim$(OpenFile.lpstrFileTitle) ' copy the filename to "Fname"

n = FileLen(OpenFile.lpstrFile) 'length of the file

End Function

Then call it with a form like:

Private Sub Form_Load()
End Sub

It's easy enough to modify anything you need to change (such as the initdir) openfile UDT

Thank you for the info; when I tried it, it opened the dialog box of teh particular folder, but when I clicked on the file to open nothin happened.

Do you know how I can install a common dialog control into my system? Because if I had that, my program will be much easier. Also the code you gave me is to long, I appreciate it very much but do you have access to a shorter code?


not without using the commondialog control..... you can download the .ocx from just about anywhere..... once you have it downloaded, place it in the windows\system32 folder, and click start, run, regsvr32 c:\windows\system32\comdlg32.ocx, and hit enter..... then you should have it on the system. I personally suggest the API method over the ocx, though, since the api method is standalone, and doesn't require such dependancies.... but whatever boats your float.

Thank you very much.


This is what I have; do you think you could figure a way I could specify which directory I want?




Dim UF As Variant
UF = Application.GetOpenFileName(FileFilter:="Excel workbooks(*.xls), *.xls", Title:="Excel Files")
If UF = False Then
Exit Sub
If UF <> "" Then
Workbooks.Open Filename:=UF
End If
End If

You would be better off calling the premade function SelectFileOpenDialog, as is in the attachment, but your call getopenfilename should be passed a variable of the OPENFILENAME UDT. So, maybe you want to make one of those, and set all it's elements before calling the API.

Please could you help me write code? Thanks.

Also I have another one that works using FileDialog, as the top code, it opens the dialog but will not open the file. please make the right corrections for me (code).

Dim FD As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set FD = Application.FileDialog(msoFileDialogFilePicker)

FD.Title = "Choose file to open"
FD.InitialFileName = "H:\"


I dunno, I've never used the FileDialog object. It's not a standard part of VB6, and I dunno where you got it. you should be able to call a property that gets set when the file is selected, maybe something like FD.Filename, but since I don't use it, I wouldn't know....

In the first example, it looks like you are trying to open the file in excel.... which would be something more like:

dim objExcel
dim objworkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(file_from_dialogbox_here)
objExcel.visible = true

set objExcel = nothing
set objWorkbook = nothing

I tried what you said about registering my comdlg32.ocs; it said successful, but when I tried drawing the dialog box on the form I get this error " This control could not be created because it is not properly registered." What can I do?


did you do comdlg32.ocs, or comdlg32.ocx? If so, you may have to download and run all the vbruntimes.... did you read the page I sent?

Just in case anyone else came across this like I did, it is brilliant but needs "End If" inserted above "End Function" :)

I'm not sure what version of VB you are using but in VB6, to get the common dialog control, you need to go to your top menu, Project, then Components, then look for Microsoft Common Dialog Control.6.0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.