TreeView1

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi I have been trying to load folder and sub folders and file, I can first
level folders but having problem trying to get second etc.
If anybody could it would be much appreciated.
Charles
 
Try code like the following in the userform that contains the TreeView:


Private Sub CommandButton1_Click()
Dim TopFolderStr As String
Dim TopFolder As Scripting.Folder
Dim FSO As Scripting.FileSystemObject
Dim TopNode As Node

TopFolderStr = "C:\TopLevelFolder"
Set FSO = New Scripting.FileSystemObject
Set TopFolder = FSO.GetFolder(TopFolderStr)

Set TopNode = UserForm1.TreeView1.Nodes.Add(Text:=TopFolder.Name)
DoFolder FSO, UserForm1.TreeView1, TopFolder, TopNode
End Sub


Private Sub DoFolder(FSO As Scripting.FileSystemObject, _
TVX As TreeView, WhatFolder As Scripting.Folder, _
WhatFolderNode As Node)

Dim FF As Scripting.Folder
Dim F As Scripting.File
Dim FFNode As Node
Dim FNode As Node
Dim SubFF As Scripting.Folder

For Each FF In WhatFolder.SubFolders
Set FFNode = TVX.Nodes.Add(WhatFolderNode, tvwChild, , FF.Name)
For Each F In FF.Files
Set FNode = TVX.Nodes.Add(FFNode, tvwChild, , F.Name)
Next F
For Each SubFF In WhatFolder.SubFolders
DoFolder FSO, TVX, FF, FFNode
Next SubFF
Next FF
End Sub




--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
Thanks Chip I do have one question when I run the code I get duplicate
Folders [6 times] I have stepped through the code but can't see where it's
doing it?
Regards
Charles
PS I like the code I was using different function to try and do the same
thing.
 
Thamks Chip I have work out what was caused the Dups I removed For Each SubFF
In WhatFolder.SubFolders and it was okay
Regards
Charles

vqthomf said:
Thanks Chip I do have one question when I run the code I get duplicate
Folders [6 times] I have stepped through the code but can't see where it's
doing it?
Regards
Charles
PS I like the code I was using different function to try and do the same
thing.

vqthomf said:
Hi I have been trying to load folder and sub folders and file, I can first
level folders but having problem trying to get second etc.
If anybody could it would be much appreciated.
Charles
 
Thamks Chip I have work out what was caused the Dups I removed For Each
SubFF
In WhatFolder.SubFolders and it was okay

You need that code if you are looping through an arbitrary level of nest
folders and files.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

vqthomf said:
Thamks Chip I have work out what was caused the Dups I removed For Each
SubFF
In WhatFolder.SubFolders and it was okay
Regards
Charles

vqthomf said:
Thanks Chip I do have one question when I run the code I get duplicate
Folders [6 times] I have stepped through the code but can't see where
it's
doing it?
Regards
Charles
PS I like the code I was using different function to try and do the same
thing.

vqthomf said:
Hi I have been trying to load folder and sub folders and file, I can
first
level folders but having problem trying to get second etc.
If anybody could it would be much appreciated.
Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top