GetOpenFilename type mismatch

X

XLHead

Hi,

I am new to this group (and programming).

I have a userform with a listbox that is to be populated with user
selected filenames.

When the user clicks a button on the worksheet I have the the userform
open, the Activate event calls a macro with GetOpenFilenames
(Multiselect = True).

I have a variable called arrFileNames as Variant for the array of file
names.

When I try to add the names to the ListBox I get runtime error 13 -
Type mismatch... sometimes. If the Open dialog box opens and I just
click open without selecting a file it will sometimes put the default
selected file name in the ListBox.


Here is the code that is called from the UserForm_Activate() event:

Private Sub cmdGet_Click()
' Get list of files to import and put in list box.

Dim i As Integer
Dim arrFileNames As Variant

arrFileNames = Application.GetOpenFilename(, 1, _
"Select One Or More Files To Open", , True)

If TypeName(arrFileNames) = "Boolean" Then Exit Sub 'cancel
pressed

For i = LBound(arrFileNames) To UBound(arrFileNames)
lbxFiles.AddItem funcRemovePath(CStr(arrFileNames(i)))
lbxFiles.List(lbxFiles.ListCount - 1, 1) = CStr(arrFileNames(i))
Next i

End Sub


I am getting the error at the LBound(arrFileNames) line.

Am I doing something wrong or is this just some strange bug? It's
driving me nuts!
 
J

Jon Peltier

I've never encountered this that I can recall, and I use GetOpenFileName a
lot. If you only return one file, it should be an array with one element,
but I've never explicitly tried hitting Enter without selecting a file
first. You could check TypeName(arrFileNames) for Variant() or just
IsArray(arrFileNames) before you loop through the array.

- Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top