GetOpenFileName question

marathonlady

Registered User.
Local time
Today, 14:35
Joined
Jul 10, 2002
Messages
120
Okay, I now know how to use the GetOpenFileName to have the dialog box come up so the user can choose their file. My next question: I want a default drive and directory to appear in the file box How do I do that?

Thanks in advance.
 
Look at the parameters that are passed and the properties that are set when running this code. One of the properties gives you a default...the other gives you the type of files such as .exes, .txt's, all, etc.

Jon
 
Code:
Dim filebox As OPENFILENAME  ' open file dialog structure
    Dim fname As String          ' filename the user selected (gets complete path)
    Dim result As Long           ' result of opening the dialog (return value if file is found)
    Dim dest As String           ' constant path of where files will be uploaded
    Dim arrFileArray() As String
    Dim strFileString As String  'file name without path
   
     ' Configure how the dialog box will look
    With filebox
        ' Size of the structure.
        .lStructSize = Len(filebox)
        ' Handle to window opening the dialog.
            .hwndOwner = Me.Hwnd
        ' Handle to calling instance (not needed).
        .hInstance = 0
        ' File filters to make available: Text Files and All Files
        .lpstrFilter = "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar
        '.lpstrCustomFilter is ignored -- unused string
        .nMaxCustomFilter = 0
        ' Default filter is the first one.
        .nFilterIndex = 1
        ' No default filename.  Also make room for received
        ' path and filename of the user's selection.
        .lpstrFile = Space(256) & vbNullChar
        .nMaxFile = Len(.lpstrFile)
        ' Make room for filename of the user's selection.
        .lpstrFileTitle = Space(256) & vbNullChar
        .nMaxFileTitle = Len(.lpstrFileTitle)
        ' Initial directory is C:\.
        .lpstrInitialDir = "C:\" & vbNullChar
        ' Title of file dialog.
        .lpstrTitle = "Select a File" & vbNullChar
        ' The path and file must exist; hide the read-only box.
        .flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
        ' The rest of the options aren't needed.
        .nFileOffset = 0
        .nFileExtension = 0
        '.lpstrDefExt is ignored -- unused string
        .lCustData = 0
        .lpfnHook = 0
        '.lpTemplateName is ignored -- unused string
    End With
    
    ' Display the dialog box.
    result = GetOpenFileName(filebox)
    If result <> 0 Then
        'file found
        ' Remove null space from the file name.
        fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
        Debug.Print "The selected file: "; fname
    Else
       'user wants to cancel
       'MsgBox "Invalid File!", vbExclamation, "Invalid File"
       Exit Sub
    End If

I've provided the code with comments...look specifically at:

' Initial directory is C:\.
.lpstrInitialDir = "C:\" & vbNullChar

and:

' Handle to calling instance (not needed).
.hInstance = 0
' File filters to make available: Text Files and All Files
.lpstrFilter = "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar


Jon
 
I get an error on Dim Filebox as OPENFILENAME

Compile error
User-defined type not allowed.

Is this a reference problem?
If so, how do I correct it?

Thanks for all your help.
 

Users who are viewing this thread

Back
Top Bottom