取消选项GetOpenFilename(Multiselect:= True) [英] Cancel Option GetOpenFilename(Multiselect:= True)

查看:127
本文介绍了取消选项GetOpenFilename(Multiselect:= True)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:当我将 GetFileOpenFileName 选项与 Multiselect = True ,如果我选择一个或多个文件,它将以数组形式返回结果,但是如果单击取消",它将以布尔变量类型返回.我应该怎么做才能避免

I have the following question: when I use the GetFileOpenFileName option with Multiselect = True it returns the results as a Array if I selected one file or more, but if I click "Cancel" it returns as a boolean vartype. What should I do to avoid the

错误13类型不兼容

error 13 "Incompatible Type

当有人单击它时.

此外,我已经尝试测试 if(vartype(filename)= vbBoolean)然后 if(filename = False)然后退出sub,但是第一个我遇到了同样的错误,第二个错误则是如果选择某些文件,则不允许为 filename 赋值.

Besides, I already tried to test if(vartype(filename) = vbBoolean) then or if(filename = False) then to exit sub, but the first one I took the same error and the second one it said that I'm not allowed to assign values to filename if I select some file.

这是代码.

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

推荐答案

根据@Brian M Stafford和@braX的评论,您的代码应进行如下修改...

As per comments from both @Brian M Stafford and @braX, your code should be amended as follows...

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*")

    If Not IsArray(filename) Then
        MsgBox "User cancelled!", vbExclamation 'optional
        Exit Sub
    End If

    For i = 1 To UBound(filename)
        MsgBox filename(i)
    Next i

End Sub

为澄清起见,请注意,文件名声明为 Variant ,而不是声明为元素为 Variant 数据类型的数组.

To clarify, notice that filename is declared as Variant, not as an array whose elements are a Variant data type.

这样,<代码>文件名可以被分配包含文件名的,当一个或多个文件被选择的阵列时,或者当用户取消的布尔值.

As such, filename can be assigned either an array containing the filenames when one or more files are selected, or a boolean value when the user cancels.

还要注意,我们测试了 filename 是否为数组,以确定用户是否选择了一个或多个文件.如果不是,则退出子程序.否则,它将继续.

Also notice that we test whether filename is an array to determine whether the user has selected one or more files. If not, it exits the sub. Otherwise, it continues.

这篇关于取消选项GetOpenFilename(Multiselect:= True)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆