Option "Cancel" Getopenfilename(Multiselect:= True)

Asked

Viewed 83 times

0

i have the following doubt, I want to use the code with the property Multiselect = True which returns a Variant() as a result, however when I click "cancel" it returns False, resulting in the following error:

Runtime error 13 "incompatible types".

Code below:

public sub open_file()
dim i as integer
Dim filename() As Variant

filename = Application.GetOpenFilename(Title:="Arquivos em Excel", MultiSelect:=True, FileFilter:="Arquivos em Excel,*.xls*")

For i = 1 To UBound(filename)

    msgbox filename(i)

next i

end sub

Any idea how to fix this error?

1 answer

0

Problem

With Multiselect:=True, filename will be a vector or a boolean, so types are uncopyable and a conditional needs to be created to verify which data type is returned.

If the "Cancel" button is clicked, filename is the boolean False:

Booleana False

If a file or more is selected, filename is an array with path strings for the selected files.

Vetor

Code

Public Sub open_file()
    Dim i As Long
    Dim filename As Variant
    
    filename = Application.GetOpenFilename(Title:="Arquivos em Excel", MultiSelect:=True, FileFilter:="Arquivos em Excel,*.xls*")

    If IsArray(filename) Then
        For i = 1 To UBound(filename)
            MsgBox filename(i)
        Next i
    ElseIf filename = False Then
        MsgBox "Nenhum arquivo foi selecionado."
        Exit Sub
    End If
End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.