Initial folder of open file dialog

exaccess

Registered User.
Local time
Today, 11:46
Joined
Apr 21, 2013
Messages
287
Hello Experts,
I am trying to display an open file dialog window so that the user can pick up a file. I wish the window to show a specific folder. How can I do this? The code I am using is below. The parameter InitialFileName has no effect on the outcome. Any help is welcome.

Code:
Function GetFileName(strPath As String, imtype As String) As String
On Error GoTo Err_GetFileName
    Dim Dlg As FileDialog
    Dim sfl As FileDialogSelectedItems
    Dim sflitem As Variant
    Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
    Set sfl = Dlg.SelectedItems
    GetFileName = " "
    Dlg.title = "Select the " & imtype & " file you want to access"
    Dlg.InitialFileName = strPath
    Dlg.ButtonName = "Select " & imtype
    Dlg.AllowMultiSelect = False
    Dlg.Filters.Clear
    Dlg.Filters.Add "Accepted", "*." & imtype
    
    If Dlg.Show = True Then
        sflitem = Dlg.SelectedItems(1)
        GetFileName = CStr(sflitem)
    Else
        MsgBox "no item selected - cancelled", , G_title
        GoTo Err_GetFileName
    End If
    Exit Function
Err_GetFileName:
    MsgBox Err.Description & " " & Err
    GetFileName = "ERROR"
    Exit Function
Ex_GetFileName:
    Set Dlg = Nothing
    Set sfl = Nothing
End Function
 
What is the value of strPath when the operation fails?
 
Hello Experts,
I am trying to display an open file dialog window so that the user can pick up a file. I wish the window to show a specific folder. How can I do this? The code I am using is below. The parameter InitialFileName has no effect on the outcome. Any help is welcome.

Code:
Function GetFileName(strPath As String, imtype As String) As String
On Error GoTo Err_GetFileName
    Dim Dlg As FileDialog
    Dim sfl As FileDialogSelectedItems
    Dim sflitem As Variant
    Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
    Set sfl = Dlg.SelectedItems
    GetFileName = " "
    Dlg.title = "Select the " & imtype & " file you want to access"
    Dlg.InitialFileName = strPath
    Dlg.ButtonName = "Select " & imtype
    Dlg.AllowMultiSelect = False
    Dlg.Filters.Clear
    Dlg.Filters.Add "Accepted", "*." & imtype
 
    If Dlg.Show = True Then
        sflitem = Dlg.SelectedItems(1)
        GetFileName = CStr(sflitem)
    Else
        MsgBox "no item selected - cancelled", , G_title
        GoTo Err_GetFileName
    End If
    Exit Function
Err_GetFileName:
    MsgBox Err.Description & " " & Err
    GetFileName = "ERROR"
    Exit Function
Ex_GetFileName:
    Set Dlg = Nothing
    Set sfl = Nothing
End Function

I believe the reference should be
Code:
Dlg.InitialDirectory

This would work in VB but I haven't done it VBA. But go ahead and try it.

Best,
Jiri
 
I have successfully used the .InitialFileName property of the file system object without having to use the .InitialDirectory property. In fact, using the object browser on the appropriate library, there IS no .InitialDirectory property.

You do have to set up the filters even if they merely say "*.*" and don't specify a particular file type or name. As to whether you have anything selected, the correct test (to the best of my understanding) is whether the .FileDialogSelectedItems.Count is or is not zero. Even if the multi-select option is false, this will be zero or one.
 
I have successfully used the .InitialFileName property of the file system object without having to use the .InitialDirectory property. In fact, using the object browser on the appropriate library, there IS no .InitialDirectory property.

You do have to set up the filters even if they merely say "*.*" and don't specify a particular file type or name. As to whether you have anything selected, the correct test (to the best of my understanding) is whether the .FileDialogSelectedItems.Count is or is not zero. Even if the multi-select option is false, this will be zero or one.

Correct. I have re-checked and found the same. The FileDialog object in the Office library is somewhat different thn the VB Net. I have tested it with the .InitialFileName and it works just fine.

Best,
Jiri
 
What is the value of strPath when the operation fails?

It is D:\MyWork\ACEXWO\ACCESS\TelephoneDB\ContactFiles\
Contacts is a directory. Deleting the last slash does not change anything.
 
Last edited:
I have successfully used the .InitialFileName property of the file system object without having to use the .InitialDirectory property. In fact, using the object browser on the appropriate library, there IS no .InitialDirectory property.

You do have to set up the filters even if they merely say "*.*" and don't specify a particular file type or name. As to whether you have anything selected, the correct test (to the best of my understanding) is whether the .FileDialogSelectedItems.Count is or is not zero. Even if the multi-select option is false, this will be zero or one.

Neither InitialDirectory no InitialFileName work. The second one is already in the code.
 
Neither InitialDirectory no InitialFileName work. The second one is already in the code.

I just copied your code, set strPath to "C:\Temp" in a module and called your code, and it opens to that folder.

However variable G_title does not exist?

Also if the folder does not exist, mine defaults to 'My Documents'

Your previous post for path is incorrect as you mention Contacts as a folder, but you have ContactFiles? or is that a typo?, as I would have copied and pasted that text.
 
I just copied your code, set strPath to "C:\Temp" in a module and called your code, and it opens to that folder.

However variable G_title does not exist?

Also if the folder does not exist, mine defaults to 'My Documents'

Your previous post for path is incorrect as you mention Contacts as a folder, but you have ContactFiles? or is that a typo?, as I would have copied and pasted that text.

G_title is a global variable. It is defined and has a string value.

ContactFiles is the folder where the data resides. Contacts is a typo. I saw it and edited the post.

It also works when I write "D:\MyWork" but defaults to My Documents folder when I write it as it is in my previous post.

The problem persists. I am missing something but what. I can not figure out.
 
Well I would be stepping through the code and checking each variable in your function.

What is happening is coincidentally what happens when the folder does not exist.?

FWIW I tried with trailing "\" and without and it works both times.

When you get to the bottom of it, I'd be interested to know what it was. :D
 
Re: Initial folder of open file dialog -SOLVED

OK folks here is the solution. When I installed the Operating System I had changed the pointer of the My Documents folder to MyWork. However the system kept the My Documents folder and assigned an alias to MyWork as MyDocuments without space. Now although the Windows Explorer sees the MyWork as replacement the Free Commander sees it as MyDocuments. This gave me the idea to search in that direction. Finally I replaced MyWork with My Documents and the Open Dialog worked.
Thanks to All who tried to help.
 
Ah, I just use Windows Explorer anyway, but well spotted.
 

Users who are viewing this thread

Back
Top Bottom