放一个Command到Form上就可以运行了! Private Const OFN_ALLOWMULTISELECT As Long = &H200Private Const OFN_EXPLORER As Long = &H80000 Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long 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 TypePrivate Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPrivate OFN As OPENFILENAME Private Function StripDelimitedItem(startStrg As String, delimiter As String) As String Dim pos As Long pos = InStr(1, startStrg, delimiter) If pos Then StripDelimitedItem = Mid$(startStrg, 1, pos) startStrg = Mid$(startStrg, pos + 1, Len(startStrg)) End IfEnd FunctionPrivate Sub Command1_Click() Dim buff As String With OFN .lStructSize = Len(OFN) .hwndOwner = Form1.Hwnd .lpstrFilter = "Text Files" & vbNullChar & "*.txt" .nFilterIndex = 2 .lpstrFile = "*.txt" & Space$(1024) & vbNullChar & vbNullChar .nMaxFile = Len(.lpstrFile) .lpstrDefExt = "txt" & vbNullChar & vbNullChar .lpstrFileTitle = vbNullChar & Space$(512) & vbNullChar & vbNullChar .nMaxFileTitle = Len(OFN.lpstrFileTitle) .lpstrInitialDir = "d:\Allen" & vbNullChar & vbNullChar .lpstrTitle = "OpenMultiFile" .flags = OFS_FILE_OPEN_FLAGS Or _ OFN_ALLOWMULTISELECT Or _ OFN_EXPLORER
End With If GetOpenFileName(OFN) Then buff = Trim$(left$(OFN.lpstrFile, Len(OFN.lpstrFile) - 2)) Do While Len(buff) > 3 MsgBox StripDelimitedItem(buff, vbNullChar) Loop End IfEnd Sub
好好看看commondlg吧,他本身就可以实现多个文件的选择的
commondialog 的一个参数可以实现选择多个文件
cdlOFNAllowMultiselect 它指定文件名列表框允许多重选择。 运行时,通过按 SHIFT 键以及使用 UP ARROW 和 DOWN ARROW 键可选择多个文件。作完此操作后,FileName 属性就返回一个包含全部所选文件名的字符串。串中各文件名用空格隔开。
Private Const OFN_ALLOWMULTISELECT As Long = &H200Private Const OFN_EXPLORER As Long = &H80000
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
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 TypePrivate Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPrivate OFN As OPENFILENAME
Private Function StripDelimitedItem(startStrg As String, delimiter As String) As String Dim pos As Long pos = InStr(1, startStrg, delimiter) If pos Then StripDelimitedItem = Mid$(startStrg, 1, pos)
startStrg = Mid$(startStrg, pos + 1, Len(startStrg)) End IfEnd FunctionPrivate Sub Command1_Click()
Dim buff As String With OFN .lStructSize = Len(OFN)
.hwndOwner = Form1.Hwnd
.lpstrFilter = "Text Files" & vbNullChar & "*.txt"
.nFilterIndex = 2
.lpstrFile = "*.txt" & Space$(1024) & vbNullChar & vbNullChar
.nMaxFile = Len(.lpstrFile)
.lpstrDefExt = "txt" & vbNullChar & vbNullChar
.lpstrFileTitle = vbNullChar & Space$(512) & vbNullChar & vbNullChar
.nMaxFileTitle = Len(OFN.lpstrFileTitle)
.lpstrInitialDir = "d:\Allen" & vbNullChar & vbNullChar
.lpstrTitle = "OpenMultiFile"
.flags = OFS_FILE_OPEN_FLAGS Or _
OFN_ALLOWMULTISELECT Or _
OFN_EXPLORER
End With
If GetOpenFileName(OFN) Then buff = Trim$(left$(OFN.lpstrFile, Len(OFN.lpstrFile) - 2))
Do While Len(buff) > 3 MsgBox StripDelimitedItem(buff, vbNullChar) Loop
End IfEnd Sub
运行时,通过按 SHIFT 键以及使用 UP ARROW 和 DOWN ARROW 键可选择多个文件。作完此操作后,FileName 属性就返回一个包含全部所选文件名的字符串。串中各文件名用空格隔开。
to Tenner:
不知道你如何实现的,可否有更好的方法,能否看看你的代码
[email protected] 谢谢