Link to home
Start Free TrialLog in
Avatar of Dale Logan
Dale LoganFlag for United States of America

asked on

Capture file name only in FileDialogFilePicker

Experts,

I am trying to figure out how to use FileDialogFilePicker, but just can't do it. I have a button on a form that I want to allow users to select am image file. I then want that file name entered into a field (ProfilePicName) on the same form. Sounds simple, but I am just too rusty at VBA.

Thanks for any help.

Dale
ASKER CERTIFIED SOLUTION
Avatar of bfuchs
bfuchs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
Norie

Dale

Not sure how you are using the FileDialogFilePicker but perhaps this will help.
Dim strFileName As String

    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            strFileName = Mid(.SelectedItems(1), InStrRev(.SelectedItems(1), "\") + 1)
        End If
    End With

    If strFileName <>"" Then
        MsgBox "You picked: " & strFileName
        
        ' code to enter filename in table
      
    Else
        MsgBox "No file picked
    End If

Open in new window

You may simply have this function in the Module.
Remember to add the reference to the Microsoft Office 16.0 Object Library (16.0 belongs to Excel 2016, find the relevant version in the available references).
Function getFileName() As String
Dim FileName As String
With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select An Image File"
    .Filters.Clear
    .Filters.Add "Image Files", "*.jpg;*.gif;*.png"
    .AllowMultiSelect = False
    If .Show <> 0 Then
        FileName = .SelectedItems(1)
        FileName = Mid(FileName, InStrRev(FileName, "\") + 1)
        getFileName = FileName
    Else
        getFileName = "No File Selected"
    End If
End With
End Function

Open in new window

And then you may use this function in your code like this...

'Assuming you have declared a variable to hold the image file name
Dim strImageFile As String
strImageFile = getFileName

Open in new window

Avatar of Dale Logan

ASKER

Thanks for the help. I had already gone with the first solution. Thanks to the others for your suggestions as well. I am sure I will be using pieces of those as well.