open file browser to specific folder and get path (1 Viewer)

Cowboy_BeBa

Registered User.
Local time
Today, 18:10
Joined
Nov 30, 2010
Messages
188
hi all

im trying to open the windows filebrowser (preferably to a specific folder, if folder doenst exist then it can open to wherever)

Once the user selects a file id like to be able to store the filename and path in a variable and mess around with it later (preferably would like to store the name separately from the path but not completely necessary)

after some googling i found the following code which does invoke the file browser, but it does not show me the name/path of the file chosen
all it does is give me a count of the items selected (tried changing SelectedItems.count to .value but that caused a runtime error)

Code:
    Dim f    As Object
    Set f = Application.FileDialog(3)
    Dim path As String
    f.AllowMultiSelect = False
    f.Show
    
    
    
    MsgBox "file choosen = " & f.SelectedItems.Count
 

MarkK

bit cruncher
Local time
Today, 03:10
Joined
Mar 17, 2004
Messages
8,178
SelectedItems is a collection, so the first item in that collection can be addressed with a zero or a one, I forget which...
Code:
msgbox f.SelectedItems.Item(0)
 

Cowboy_BeBa

Registered User.
Local time
Today, 18:10
Joined
Nov 30, 2010
Messages
188
SelectedItems is a collection, so the first item in that collection can be addressed with a zero or a one, I forget which...
Code:
msgbox f.SelectedItems.Item(0)


thanks MarkK, that makes alotta sense
ive changed the code but am getting a runtime error 5 "Invalid procedure call or argument" on that line
what do ya think might be triggering that?

also, is there a way to store the file name in a separate variable to the path?

EDIT
actually i just managed to find a solution, did a bit more googling and found some code that worked out

Code:
Public Sub BrowseFile2()
    Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant
    Dim P As String

    Set f = Application.FileDialog(3)
    f.AllowMultiSelect = False
    If f.Show Then
        For Each varItem In f.SelectedItems
            strFile = Dir(varItem)
            strFolder = Left(varItem, Len(varItem) - Len(strFile))
            P = strFolder & strFile
        Next
    End If
    Set f = Nothing
    
    MsgBox (strFile)
    MsgBox (strFolder)
    
End Sub
 
Last edited:

MarkK

bit cruncher
Local time
Today, 03:10
Joined
Mar 17, 2004
Messages
8,178
...selects a file...
This looks singular. Seems there is no need to enumerate the collection if you have explicitly set...
Code:
    f.AllowMultiSelect = False
But you're good? It's working?

Check out the VBA.Split() function too, if you need to split up a delimited string.

Cheers,
 

Cowboy_BeBa

Registered User.
Local time
Today, 18:10
Joined
Nov 30, 2010
Messages
188
thanks, yeah ive tested it a few times and its working
however two things just occured to me

I would ideally like to be able to restrict the user from selecting any file other than image files
i would also like to find a way to force the user to make a valid selection (either by disabling "Cancel" and exit buttons or by catching it when a user does and forcing them in a loop until they do make a valid selection
 

MarkK

bit cruncher
Local time
Today, 03:10
Joined
Mar 17, 2004
Messages
8,178
I would ideally like to be able to restrict the user from selecting any file other than image files
How do you distinguish those files from other files? You can filter by extension I think. But "image files" is pretty broad. What are the specific criteria you would use to construct this filter?
i would also like to find a way to force the user to make a valid selection (either by disabling "Cancel" and exit buttons or by catching it when a user does and forcing them in a loop until they do make a valid selection
I wouldn't "force" the user to do anything. Cancel is fair. They might have picked the option by mistake. Give them an out.
 

Users who are viewing this thread

Top Bottom