|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 Rivaadi 于 2024-12-14 23:29 编辑
vba treeview显示文件夹和子文件夹
Private Sub UserForm_Initialize()
Dim rootnode As node
Dim startFolder As String
' 设置起始文件夹路径
startFolder = "C:\Users\38027\Desktop"
' 添加根节点
Set rootnode = Me.TreeView1.Nodes.Add(, , "Root", startFolder)
rootnode.Text = "Root Folder"
' 显示文件夹和子文件夹
DisplayFolders rootnode, startFolder
End Sub
Private Sub DisplayFolders(ByVal rootnode As node, ByVal folderPath As String)
Dim fso As Object
Dim folder As Object
Dim subfolder As Object
Dim node As node
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
For Each subfolder In folder.SubFolders
Set node = rootnode.Nodes.Add(rootnode.Key, tvwChild, tvwChild + CStr(subfolder.Files.Count), subfolder.Path)
node.Text = subfolder.Name
DisplayFolders node, subfolder.Path
Next
Set node = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
[backcolor=rgba(245, 246, 249, 0.4)]想要在treeview控件上显示文件夹和子文件夹,但是代码显示
求助大神帮忙找一下问题的原因。
|
|