1,488
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
'这是从多文件名字符串分离出文件名的函数
Type DlgFileInfo
iCount As Long
sPath As String
sFile() As String
End Type
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As Long
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 Type
Public Function GetDlgFileInfo(strFile As OPENFILENAME) As DlgFileInfo
Dim sPath, tmpStr As String
Dim sFile() As String
Dim iCount As Integer
Dim I As Integer
Dim strFilename As String
strFilename = strFile.lpstrFile
On Error GoTo ErrHandle
tmpStr = OpenFile.lpstrFileTitle
If Left$(tmpStr, 1) = Chr$(0) Then
'Multiple Select
For I = 1 To Len(tmpStr)
If Mid$(tmpStr, I, 1) = Chr$(0) Then
iCount = iCount + 1
ReDim Preserve sFile(iCount) 'redim array
Else
sFile(iCount) = sFile(iCount) & Mid$(tmpStr, I, 1)
End If
Next I
Else
'single select
iCount = 1
ReDim Preserve sFile(iCount)
If Left$(tmpStr, 1) = "\" Then tmpStr = Right$(tmpStr, Len(tmpStr) - 1)
sFile(iCount) = tmpStr
End If
GetDlgFileInfo.iCount = iCount
ReDim GetDlgFileInfo.sFile(iCount)
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
GetDlgFileInfo.sPath = sPath
For I = 1 To iCount
GetDlgFileInfo.sFile(I) = sFile(I)
Next I
Exit Function
ErrHandle:
MsgBox "function run error1!", vbOKOnly + vbCritical, "custom error information!"
End Function
'这是过程中的调用
Private Sub Command5_Click()
Dim OpenFile As OPENFILENAME
Dim IReturn As Long
Dim sFilter As String
sFilter = "*.*" & Chr(0)
With OpenFile
.lStructSize = Len(OpenFile)
.hwndOwner = Form1.hWnd
.hInstance = App.hInstance
.lpstrFilter = sFilter
.nFilterIndex = 1
.lpstrFile = String(123456789, 0)
.nMaxFile = Len(.lpstrFile) - 1
.nMaxFileTitle = .nMaxFile
.lpstrInitialDir = "c:\"
.lpstrTitle = "Use the comdlg API not the ocx"
.flags = OFN_ALLOWMULTISELECT
End With
Dim DlgInfo As DlgFileInfo
Dim I As Integer
List1.ListItems.Clear
DlgInfo = GetDlgFileInfo(OpenFile)
For I = 1 To DlgInfo.iCount
List1.ListItems.Add.Text = DlgInfo.sPath & DlgInfo.sFile(I)
Next I
End Sub
'我把文件名读取出来放置在listview中
'点击后提示byref参数类型不符