在表单中添加一个公共对话框对象,然后浏览到该文件夹并将其返回到字符串变量txtOpenPath
- Dim objCD As New FileDialog
- Dim objFileSystem As Scripting.FileSystemObject
- Dim objFile As Scripting.File
- Dim sFile As String
- '''''''''''''''''''''''''''''''''''''''
- With objCD
- .Filter = "Drawing (*.dwg)|*.dwg"
- .Title = "Choose a File in directory to be converted"
- .OwnerHwnd = 0&
- '.MultiSelect = 1
- End With
-
- Set objFileSystem = New Scripting.FileSystemObject
- sFile = objCD.ShowOpen
- Set objFile = objFileSystem.GetFile(sFile)
- txtOpenPath = objFile.ParentFolder
- Set objFile = Nothing
- Set objFileSystem = Nothing
- Set objCD = Nothing
- Exit Sub
然后我调用它来填充我的列表框中的所有文件
- Public Function FindFile( _
- ByVal sFol As String, _
- ByVal sFile As String, _
- ByVal iDirs As Integer, _
- ByVal iFiles As Integer, _
- ByVal bFound As Boolean, _
- ByRef lstBox As ListBox) _
- As Long
- '------------------------------------------------------------------------------
- '
- '
- '------------------------------------------------------------------------------
- Dim fso As New FileSystemObject
- Dim fld As Folder
- Dim tFld As Folder
- Dim tFil As File
- Dim FileName As String
- '''''''''''''''''''''''''''''''''''''''
- On Error GoTo ErrHandler
- Set fld = fso.GetFolder(sFol)
- FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or _
- vbHidden Or vbSystem Or vbReadOnly)
- While Len(FileName) <> 0
- FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName)) 'calls itself
- iFiles = iFiles + 1
- lstBox.AddItem fso.BuildPath(fld.Path, FileName) ' Load ListBox
- FileName = Dir() ' Get next file
- DoEvents
- Wend
- iDirs = iDirs + 1
- If fld.SubFolders.Count > 0 And bFound = True Then
- For Each tFld In fld.SubFolders
- DoEvents
- FindFile = FindFile + FindFile(tFld.Path, sFile, iDirs, iFiles, True, lstBox)
- Next
- End If
-
- ErrHandler:
- Select Case Err.Number
- Case 0
- Err.Clear
- Case Else
- Debug.Print Err.Number & " " & Err.description
- Err.Clear
- End Select
- End Function
|