PDA

View Full Version : [SOLVED] Populate Treeview with specified Windows Directory & Sub-folders/files



lkelsen
05-14-2015, 03:42 PM
Hey team,

Fist post on VBA Express so here goes.

The following code successfully populates my TreeView object "tvProjects" on my userform with Parent nodes which are generated from folder names within a specified window directory:


Private Sub UserForm_Initialize()

Dim tvsPath As String

'TreeView Function
tvsPath = dir("I:\02 Clients\" & Range("Q1") & "\Projects\" & Range("Q2") & "\*", vbDirectory)

While tvsPath <> ""
tvProjects.Nodes.Add Text:=tvsPath
tvsPath = dir
Wend

End SubMy issue is, I need to then add all sub-folders within each folder inside the windows directory to each relevant parent node as it would look in a windows explorer directory tree.

I Have searched for this solution on this board and all over the internet but no success in finding a solution. I cant even add a test node to the parent directory when trying to use the parent nodes text or key value etc.

I hope this all makes sense, I am pretty novice with VBA programming.

Im using windows 7
VBA 7.1
VBA Knowledge: Beginner
I only have Microsoft Treeview Control, version 5.0 (SP2) installed on my machine and i believe on all other machines in the office.

Cheers,


Luke Kelsen

mancubus
05-14-2015, 11:29 PM
welcome to the forum.

you can download a ready to use file here.

http://www.cpearson.com/excel/foldertreeview.aspx

lkelsen
05-17-2015, 07:56 PM
Thank you mancubus,

I have looked at this and thought it would be a bit complicated for my experience. I have however downloaded the example worksheet and could easily change it to visually suit my application.

Only thing I am stuck on is that I want to replace the "Browse for Folder" part of this application with an already defined directory path which would be a string in cell "S1" of my worksheet.

Would you know how to go about this? Would it be easier if you had a copy of my Excel workbook and code?

Cheers,


Luke Kelsen

mancubus
05-18-2015, 12:21 AM
you are welcome.

replace
TopFolderName = BrowseFolder("Select A Folder For The TreeView")

with
TopFolderName = Worksheets("WSNameHere").Range("S1").Value

in
Private Sub btnLoadTree_Click()

lkelsen
05-18-2015, 06:09 PM
you are welcome.

replace
TopFolderName = BrowseFolder("Select A Folder For The TreeView")

with
TopFolderName = Worksheets("WSNameHere").Range("S1").Value

in
Private Sub btnLoadTree_Click()

Thanks again, both of your solutions have proved successful :yes

Appreciate your input, I was stuck for over a week at work trying to sort this out! How do I mark this as solved again?

Cheers,


Luke

Aussiebear
05-18-2015, 10:40 PM
Select Thread Tools option and go from there

lkelsen
05-19-2015, 12:53 PM
Select Thread Tools option and go from there

Cheers Aussiebear :content: