Get file name an path using VBA

I have a browse button on an Excel form so user can select a file to load.  The code below works great in Excel 2010, but causes a run-time error for users of Excel 2013.

Public Function ChooseFile() As String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Show Windows Dialog box for users to choose an Item file.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
    .AllowMultiSelect = False
    ' Set the title of the dialog box.
    .Title = "Please select the file."
    ' Clear out the current filters, and add our own.
    .Filters.Clear
    .Filters.Add "All Files", "*.*"

    ' Show the dialog box. If the .Show method returns True, the
    ' user picked at least one file. If the .Show method returns
    ' False, the user clicked Cancel.
    If .Show = True Then
    ChooseFile = .SelectedItems(1)
    txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox
    Debug.Print "From dialog " & txtFileName

    End If
    End With
End Function

Open in new window

rrhandle8Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
James ElliottConnect With a Mentor Managing DirectorCommented:
Try taking out the 'office' part of office.filedialog.
0
 
frankhelkConnect With a Mentor Commented:
Hmmm ... looks valid ... what's the error message ?
0
 
James ElliottConnect With a Mentor Managing DirectorCommented:
Also try .selecteditems(0)
0
 
rrhandle8Author Commented:
The problem was a reference to a grid control which did not exist on his computer.  Why it showed up when I tried to browse for files is strange.
0
All Courses

From novice to tech pro — start learning today.