如果您急需它,为什么不从头开始创建它呢?这有点耗时但很简单。我记得做过一次...... – Siddharth Rout 39 分钟前
这是我为您创建的一个简单示例(花了大约 40 分钟创建它)。
如下图所示创建一个用户表单,然后按所示命名。
用户表单代码
将此代码粘贴到用户表单中
Option Explicit
Dim justStarted As Boolean
Private Sub UserForm_Initialize()
With ListBox1
.ColumnCount = 2
.ColumnWidths = "70;60"
.ListStyle = fmListStylePlain
End With
justStarted = True
End Sub
Private Sub UserForm_Activate()
justStarted = False
Populate
End Sub
'~~> Manually changing folder
Private Sub InitialPath_Change()
If InitialPath = "" Or justStarted = True Then Exit Sub
If Dir(InitialPath) <> "" Then
Populate
Else
ListBox1.Clear
TextBox2.Text = ""
End If
End Sub
'~~> Listbox Single Click - File Selection
Private Sub ListBox1_Click()
If ListBox1.ListIndex < 0 Then Exit Sub
If ListBox1.List(ListBox1.ListIndex, 1) = "File" Then _
TextBox2.Text = ListBox1.List(ListBox1.ListIndex)
End Sub
'~~> Listbox Double Click - Folder Open
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If ListBox1.ListIndex < 0 Then Exit Sub
If ListBox1.List(ListBox1.ListIndex, 1) = "Folder" Then
If Right(Me.InitialPath, 1) <> "\" Then
InitialPath = Me.InitialPath & "\" & ListBox1.List(ListBox1.ListIndex, 0) & "\"
Else
InitialPath = Me.InitialPath & ListBox1.List(ListBox1.ListIndex, 0) & "\"
End If
Populate
End If
End Sub
'~~> Open Button
Private Sub CommandButton1_Click()
If Len(Trim(TextBox2.Text)) = 0 Then Exit Sub
If Right(Me.InitialPath, 1) <> "\" Then InitialPath = Me.InitialPath & "\"
If Dir(InitialPath & TextBox2.Text) <> "" Then
MsgBox "You selected " & InitialPath & TextBox2.Text
Else
MsgBox "Please select a valid file"
End If
End Sub
'~~> Exit Button
Private Sub CommandButton2_Click()
Unload Me
End Sub
'~~> Populate Listbox
Sub Populate()
Dim sFile As Variant, sFolder As Variant
Dim sFilter As String
Dim pos As Long: pos = 0
ListBox1.Clear
Dim objFSO As Object, objFolder As Object, objSubFolder As Object
Dim i As Integer
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(InitialPath)
For Each objSubFolder In objFolder.subfolders
With ListBox1
.AddItem
.List(pos, 0) = objSubFolder.Name
.List(pos, 1) = "Folder"
pos = pos + 1
End With
Next objSubFolder
sFilter = Split(Filter, "(")(1)
sFilter = Split(sFilter, ")")(0)
Filter = sFilter
sFile = Dir(InitialPath & Trim(sFilter))
While (sFile <> "")
With ListBox1
.AddItem
.List(pos, 0) = sFile
.List(pos, 1) = "File"
pos = pos + 1
End With
sFile = Dir
Wend
End Sub
模块
你可以从一个模块中调用它
Sub Sample()
With MyFileBrowser
.InitialPath = "C:\Users\Siddharth\Desktop\"
.Filter = "My Files,(*ture*.*)"
.Caption = "Open"
.Show
End With
End Sub
行动中
免责声明:
- 错误处理未完成。
- 仅适用于单个过滤器
-
Filter 文本框已锁定,无法编辑