关于commondialog的多选(如何提取文件名?)
第一种方法==================================================
Function OpenFile(strFileInfor As String, strFileType As String, blnIsMulti As Boolean, OpenFileDialog As Control) As String()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'OpenFileDialog 通用对话框
'strstrfileInfor 文件提示
'strstrfileType 文件类型,扩展名
'blnblnismulti 是否为多选
'返回为文件名数组,OpenFile(0)存放文件路径,从1开始存放文件名,包括只有一个文件时的情况。
'返回的路径中已经包含了最后的"\"
'如果没有选择任何文件,OpenFileFuction(0)返回为""
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'设置打开文件对话框过滤器
OpenFileDialog.Filter = strFileInfor & "|" & strFileType
'设定是否可以多选
If blnIsMulti Then
OpenFileDialog.Flags = &H80200
OpenFileDialog.MaxFileSize = 1024 '设置使用CommonDialog控件被打开的文件名的最大尺寸
Else
OpenFileDialog.Flags = 0
End If
OpenFileDialog.ShowOpen
'提取各文件名
Dim tempStr As String
Dim tempFileName() As String
tempStr = OpenFileDialog.FileName
'如果用户取消了,或没有选择任何文件,处理返回仍为数组形式,OpenFileFuction(0)为""
If tempStr = "" Then
ReDim tempFileName(0)
tempFileName(0) = ""
OpenFile = tempFileName
Exit Function
End If
Dim P, N As Long
P = 0 '分隔字符位置
N = 0 '文件个数
'提取路径
ReDim tempFileName(1) '至少有一个文件
'P = InStr(1, tempStr, " ") '为UNICODE
P = InStr(1, tempStr, Chr$(0)) '为UNICODE
Debug.Print tempStr & " p=" & P
If P = 0 Then '如果只选择了一个文件
P = InStrRev(tempStr, "\") 'P取得"\"所在位置
tempFileName(0) = Left$(tempStr, P - 1)
tempFileName(1) = Mid$(tempStr, P + 1)
tempFileName(0) = IIf(Right$(tempFileName(0), 1) <> "\", tempFileName(0) & "\", tempFileName(0))
' tempFileName(0) = GetFilePath(tempStr)
' tempFileName(1) = GetFileName(tempStr)
Else '选择了多个文件
Do While tempStr <> ""
P = InStr(tempStr, Chr$(0)) '为UNICODE
ReDim Preserve tempFileName(N)
'如果没有空格(只剩余一个文件)
If P = 0 Then
tempFileName(N) = tempStr
Exit Do
Else
tempFileName(N) = Left$(tempStr, P - 1)
tempStr = Mid$(tempStr, P + 1)
N = N + 1
End If
Loop
tempFileName(0) = IIf(Right$(tempFileName(0), 1) <> "\", tempFileName(0) & "\", tempFileName(0)) '处理路径
End If
'返回文件名数组
OpenFile = tempFileName
'还原为单选
OpenFileDialog.Flags = 0
'------------------------------------------------
' Exit Function
' '----------------
ToExit:
ErrorLog "modFileOption\OpenFile"
Resume Next
End Function
第二种方法==================================================
Public Function OpenFile(strFileInfor As String, strFileType As String, blnIsMulti As Boolean, OpenFileDialog As Control, StrIntdir As String) As String()
On Error GoTo ToExit '打开错误陷阱
'------------------------------------------------
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'OpenFileDialog 通用对话框
'strstrfileInfor 文件提示
'strstrfileType 文件类型,扩展名
'blnblnismulti 是否为多选
'返回为文件名数组,OpenFile(0)存放文件路径,从1开始存放文件名,包括只有一个文件时的情况。
'返回的路径中已经包含了最后的"\"
'如果没有选择任何文件,OpenFileFuction(0)返回为""
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'设置打开文件对话框过滤器
OpenFileDialog.Filter = strFileInfor & "|" & strFileType
'设定是否可以多选
If blnIsMulti Then
OpenFileDialog.Flags = &H80200
OpenFileDialog.MaxFileSize = 1024 '设置使用CommonDialog控件被打开的文件名的最大尺寸
Else
OpenFileDialog.Flags = 0
End If
OpenFileDialog.InitDir = StrIntdir
OpenFileDialog.FileName = ""
OpenFileDialog.ShowOpen
'提取各文件名
Dim tempStr As String
Dim tempFileName() As String
tempStr = OpenFileDialog.FileName
'如果用户取消了,或没有选择任何文件,处理返回仍为数组形式,OpenFileFuction(0)为""
If tempStr = "" Then
ReDim tempFileName(0)
tempFileName(0) = ""
OpenFile = tempFileName
Exit Function
End If
Dim P, n As Long
P = 0 '分隔字符位置
n = 0 '文件个数
'提取路径
tempFileName = Split(tempStr, Chr$(0))
If UBound(tempFileName) = 0 Then '选择一个文件
ReDim tempFileName(1)
tempFileName(0) = GetFilePath(tempStr) '提取路径
tempFileName(1) = GetFileName(tempStr) '取文件名
Else
选择多个文件
tempFileName(0) = IIf(Right$(tempFileName(0), 1) <> "\&#