|
|
发表于 2025-3-24 16:11
来自手机
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 lss001 于 2025-3-25 16:29 编辑
'WPS使用WindowsAPI函数GetOpenFileName请参考以下代码
Private Type OPENFILENAME
lStructSize As Long '结构长度
hwndOwner As Long '主窗口句柄
hInstance As Long '主线程句柄
lpstrFilter As String '筛选条件
lpstrCustomFilter As String '定制条件
nMaxCustFilter As Long 'lpstrCustomFilter长度
nFilterIndex As Long '筛选序列:0定制条件,1筛选条件
lpstrFile As String '初始文件名
nMaxFile As Long 'lpstrFile内存大小
lpstrFileTitle As String '初始扩展名
nMaxFileTitle As Long 'lpstrFileTitle内存大小
lpstrInitialDir As String '初始目录
lpstrTitle As String '对话框标题
flags As Long '对话框选项
nFileOffset As Integer '文件名起始偏移
nFileExtension As Integer '文件扩展名起始偏移
lpstrDefExt As String '默认文件扩展名
lCustData As Long '勾子数据
lpfnHook As Long '勾子指针
lpTemplateName As String '模板项目名
End Type
Private Declare PtrSafe Function GetOpenFileName _
Lib "comdlg32.dll" Alias "GetOpenFileNameA" ( _
pOpenfilename As OPENFILENAME) As Long
Private Declare PtrSafe Function GetSaveFileName _
Lib "comdlg32.dll" Alias "GetSaveFileNameA" ( _
pOpenfilename As OPENFILENAME) As Long
Sub WPS选择文件对话框()
Dim pfn As OPENFILENAME, pInfo&, sPath$, pPath$
Dim FileList, arr, fFile, fName, m&, n&, t&
With pfn
.lStructSize = Len(pfn)
.hwndOwner = Application.Hwnd
'.hInstance = Application.HinstancePtr
'分隔符:vbNullChar 或 Chr(0)
.lpstrFilter = "Excel Files(*.xls*)" & vbNullChar & "*.xls*"
.nFilterIndex = 1
.lpstrInitialDir = ThisWorkbook.Path
.lpstrFile = Space(254)
.nMaxFile = 40960
.lpstrTitle = "选择文件"
.flags = &H80000 + &H200 '允许多选
End With
pInfo = GetOpenFileName(pfn)
If pInfo >= 1 Then
'使用Split函数按vbNullChar拆分lpstrFile的内容为字符串组数
FileList = Split(pfn.lpstrFile, vbNullChar)
m = UBound(FileList)
n = LBound(FileList)
If m - n < 3 Then
'仅选择了一个文件,此时文件名包含路径
ReDim Preserve FileList(n)
fFile = FileList
Else
'选择了多个文件,此时路径放在数组第一个元素中,后面是不带路径的文件名
ReDim arr(m - n - 3)
sPath = FileList(LBound(FileList))
For t = LBound(FileList) + 1 To UBound(FileList) - 2
pPath = sPath & Application.PathSeparator
arr(t - LBound(FileList) - 1) = pPath & FileList(t)
Next
fFile = arr
End If
Else
End If
If IsArray(fFile) Then
For Each fName In fFile
MsgBox fName
Next
End If
End Sub |
评分
-
1
查看全部评分
-
|