954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

FileOpen Dialog without using Common Dialog Control

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.

Ini

INI
Light Poster
39 posts since May 2006
Reputation Points: 13
Solved Threads: 0
 

in a module:

' /* Code Taken From: http://forums.devx.com/showthread.php?t=148627 */
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Public Type OPENFILENAME
    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 OpenFile As OPENFILENAME
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"
Else
'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()
SelectFileOpenDialog
End Sub


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

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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?

Ini

INI
Light Poster
39 posts since May 2006
Reputation Points: 13
Solved Threads: 0
 

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.

http://www.ascentive.com/support/new/support_dll.phtml?dllname=COMDLG32.OCX

Attachments commondialog.zip (1.79KB)
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Thank you very much.

Ini

INI
Light Poster
39 posts since May 2006
Reputation Points: 13
Solved Threads: 0
 

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

Thanks.

Ini

___________________________________________________

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

INI
Light Poster
39 posts since May 2006
Reputation Points: 13
Solved Threads: 0
 

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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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:\"
FD.Show




Ini

INI
Light Poster
39 posts since May 2006
Reputation Points: 13
Solved Threads: 0
 

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
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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?

Ini

INI
Light Poster
39 posts since May 2006
Reputation Points: 13
Solved Threads: 0
 

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?

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You