I'm trying to construct a utility function to prompt the user for an arbitrary file via the standard Windows file dialog.
I would like to pass the list of filetype filters as a two dimensional array of strings, where the first element of each subarray is the filetype description and the second element is the filetype filter.
Below is my function:
'' GetFile - Lee Mac
''
'' Prompts the user to select a file using a standard Windows file dialog
''
'' msg - [str] Dialog title
'' ini - [str] Initial filename/filepath
'' flt - [arr] Array of filetype filters
''
Function GetFile(Optional strMsg As String = "Select File", Optional strIni As String = vbNullString, Optional arrFlt) As String
Dim dia As FileDialog
Set dia = Application.FileDialog(msoFileDialogFilePicker)
With dia
.InitialFileName = strIni
.AllowMultiSelect = False
.Title = strMsg
.Filters.Clear
If IsMissing(arrFlt) Then
.Filters.Add "All Files", "*.*"
Else
Dim i As Integer
For i = 0 To UBound(arrFlt, 1)
.Filters.Add arrFlt(i, 0), arrFlt(i, 1)
Next i
End If
If .show Then
GetFile = .selecteditems.Item(1)
End If
End With
End Function
This works, however, when supplying the filetype filter argument to the function, I find myself having to do something like this:
Function test()
Dim arr(1, 1) As String
arr(0, 0) = "Excel Files"
arr(0, 1) = "*.xls;*.xlsx"
arr(1, 0) = "Text Files"
arr(1, 1) = "*.txt"
GetFile , , arr
End Function
I've also tried the following but receive 'Subscript out of range':
Dim arr() As Variant
arr = Array(Array("Excel Files", "*.xls;*.xlsx"), Array("Text Files", "*.txt"))
Is there a better way to define a literal 2D string array that I'm missing?
Many thanks in advance for your advice & feedback.