Public Function SelectFile() As String
'Requires Office XP (2002) or higher
'Requires a reference to the Microsoft Office Object Library
'Created by Helen Feddema 3-Aug-2009
'Last modified 3-Aug-2009
On Error GoTo ErrorHandler
Dim fd As Office.FileDialog
Dim varSelectedItem As Variant
Dim strFileNameAndPath As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
'Set AllowMultiSelect to True to allow selection of multiple files
.AllowMultiSelect = False
.Title = "Browse for File"
.ButtonName = "Select"
.Filters.Clear
.Filters.Add "Worksheets", "*.xls; *.xlt", 1
.InitialView = msoFileDialogViewDetails
If .Show = -1 Then
'Get selected item in the FileDialogSelectedItems collection
For Each varSelectedItem In .SelectedItems
strFileNameAndPath = CStr(varSelectedItem)
Next varSelectedItem
Else
Debug.Print "User pressed Cancel"
strFileNameAndPath = ""
End If
End With
SelectFile = strFileNameAndPath
ErrorHandlerExit:
Set fd = Nothing
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Function
|