Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multi Select with GetOpenFileName (97/2000)

    Hi Folks,

    If you specify MultiSelect as True in the GetOpenFileName method, the return value is an array. However, if the user presses Cancel, the return value is False. I need a good strategy for dealing with both scenarios. The code shown below gives a type mismatch if the user Cancels.

    <pre>Dim RefFiles()
    RefFiles() = Application.GetOpenFilename(FileFilt, 2, Title, , True)
    </pre>


    The sample code in Help shows how to check for Cancel with MultiSelect set to False, but the code for checking the result has to come _after_ the GetOpenFileName method successfully returns a result to the variable. Since I can't get past the type mismatch, I can't check the result. e.g.

    <pre> RefFiles() = Application.GetOpenFilename(FileFilt, 2, Title, , True)

    If IsArray(RefFiles()) Then
    ' User picked some files
    Else
    ' User Cancelled
    End If

    </pre>


    doesn't "cut it".

    Thanks in advance!

  2. #2
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Kortrijk, Belgium
    Posts
    571
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Multi Select with GetOpenFileName (97/2000)

    This should work:

    <pre>Sub ImportMultipleFiles()
    Dim FileName
    Dim i As Integer
    Dim Title As String, Msg As String
    Title = "Select multiple files"
    FileName = Application.GetOpenFilename(Title:=Title, MultiSelect:=True)
    If Not IsArray(FileName) Then
    MsgBox "You pressed the cancel button"
    Exit Sub
    End If
    For i = LBound(FileName) To UBound(FileName)
    Msg = Msg & FileName(i) & vbCrLf
    Next i
    MsgBox Msg
    End Sub
    </pre>


    Just want to add that the reason for your problem is that you define the refiles() as an array by using the (). You should not do this. You should declare it as a variant, then when you press the cancel button, the returned object is not an array.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Multi Select with GetOpenFileName (97/2000)

    It works. I guess in my code, by putting the parentheses in the Dim statement, the variable is fixed as an array, even though it is an array of variants. Your code establishes it as a true variant, which becomes and array if the user selects, and a boolean if the cancel.

    Sheeesh! How esoteric.... Thanks !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •