Generic file selection window function in VBA

Here is a very useful file selection function for VBA, inputs sFilter a filter string i.e. “*.txt” and outputs an array of selected files including their path. The code is a modification of the example found on Microsoft’s MSDN FileDialog Object page.

'file list is a generic file choser, outputing the file names as an array
Private Function fileList(sFilter As String) As Variant
'Declare a variable as a FileDialog object.
Dim fd As FileDialog

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

Dim i As Integer
'Use a With...End With block to reference the FileDialog object.
With fd
.InitialFileName = ActivePresentation.Path
'Add a filter that includes all files.
.Filters.Add "All files", sFilter
If .Show = -1 Then
Dim fileData
ReDim fileData(.SelectedItems.Count - 1)
'Step through each string in the FileDialogSelectedItems collection.
For i = 1 To .SelectedItems.Count
fileData(i - 1) = .SelectedItems(i)
Next i
'The user pressed Cancel.
Exit Function
End If
End With

'Set the object variable to Nothing.
Set fd = Nothing
fileList = fileData
End Function