|
- Rem *************************************************************************************************************
- Rem 选择单个文件: GetFileName
- Rem 功能: 获得已经选择文件全路径,没选择的,返回:空白
- Rem 参数:KZM 可选,默认是:"Excel文件,*.xls;*.xlsx;*.xlsm"
- Rem 参数:Title 可选,提示信息,默认是:北极狐工作室
- Rem 参数:Filename 可选,开始文件夹,默认是:本表所在文件夹
- Rem 参数:StrSplitor 可选,各级文件夹分隔符
- Rem 使用方法: PathY = GetFileName(KZM:="Excel文件,*.xls,*.xlsx;*.xlsm", Title:="请选择Excel文件", filename:="", StrSplitor:="")
- Rem 作者: 北极狐工作室 QQ:14885553
- Rem *************************************************************************************************************
- Public Function GetFileName(Optional ByVal KZM = "Excel文件,*.xls;*.xlsx;*.xlsm", Optional ByVal Title As String = "", Optional ByVal filename As String = "", Optional ByVal StrSplitor As String = "") As String
- Dim X As Long
- Dim ARX
-
- Rem 为了解决WPS 不出现对话框 需要加一对: Application.ScreenUpdating
- Dim BLApp As Boolean
- BLApp = Application.ScreenUpdating
- Application.ScreenUpdating = False
-
- Rem 默认的分隔符号
- If StrSplitor = "" Then StrSplitor = ""
-
- If filename = "" Then filename = ThisWorkbook.Path
- Rem 如果文件夹路径 最后不是:[\] 则补充上去
- If Right(filename, 1) <> StrSplitor Then filename = filename & StrSplitor
- With Application.FileDialog(msoFileDialogFilePicker)
- .Filters.Clear '清除文件过滤器,注意顺序
- If InStr(KZM, ",") > 0 Then
- Rem KZM="Excel2003,*.xls|Excel2007,*.xlsx;*.xlsm|WORD文件,*.doc;*.docx"
- Rem KZM="图片文件,*.png;*.bmp;*.jpeg;*.jpg"
- ARX = Split(KZM, "|")
- For X = 0 To UBound(ARX)
- .Filters.Add Split(ARX(X), ",")(0), Split(ARX(X), ",")(1)
- Next
- End If
- Rem 注意顺序
- .Filters.Add "所有文件", "*.*", 1 '增加筛选器的项目为所有文件
- .AllowMultiSelect = False '不允许多项选择
- .InitialFileName = filename '//设置默认文件夹
- If Title = "" Then '//设置提示信息
- .Title = "北极狐工作室: 请选择文件存放的路径(文件夹)" '自定义浏览对话框的标题栏名称,默认:“浏览”
- Else
- .Title = Title
- End If
- If .Show = -1 Then
- GetFileName = .SelectedItems(1)
- Else
- GetFileName = ""
- End If
- End With
- Application.ScreenUpdating = BLApp
- End Function
复制代码 |
|