Here your code, finally.
Function GetFolderPath() As String
Dim oShell As Object
Set oShell = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please select folder", 0, "c:\\")
If Not oShell Is Nothing Then
GetFolderPath = oShell.Items.Item.Path
Else
GetFolderPath = vbNullString
End If
Set oShell = Nothing
End Function
Sub FindTextString()
Dim i As Integer
Dim szSearchWord As Variant
Dim ThisPath As String
Dim ThisName As String
Dim Savename As String
Dim sFollder As String
Dim NodeFullPath As String
sFollder = GetFolderPath
If sFollder <> vbNullString Then MsgBox sFollder
ThisPath = ThisWorkbook.Path
ThisName = ThisWorkbook.Name
Savename = ThisPath & "\" & ThisName
szSearchWord = Application.InputBox("What are you looking for ?", "Search", , 100, 100, , , 2)
If szSearchWord = False Then
Sheets("Sheet1").Select
End
End If
With Application.FileSearch
.NewSearch
.LookIn = sFollder
.FileType = msoFileTypeAllFiles
.SearchSubFolders = True
.TextOrProperty = szSearchWord
.Execute
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
ActiveSheet.Range("b" & (i + 1)) = .FoundFiles(i)
Next i
End With
Dim nod As Node
Dim MyPath, NodeName, parent As String
Dim n, k As Integer
For i = 1 To Application.FileSearch.FoundFiles.Count
k = 0
MyPath = Application.FileSearch.FoundFiles(i)
NodeFullPath = ""
Do Until InStr(1, MyPath, "\") = 0 And InStr(1, MyPath, ".") = 0
k = k + 1
If InStr(1, MyPath, "\") <> 0 Then
n = InStr(1, MyPath, "\")
NodeName = Mid(MyPath, 1, n)
MyPath = Mid(MyPath, n + 1, 500)
NodeFullPath = NodeFullPath & NodeName
Else
NodeName = MyPath
NodeFullPath = NodeFullPath & MyPath
MyPath = ""
End If
'On Error GoTo Riprendi
'populateTV
With Me.TreeView1
If k = 1 Then
On Error Resume Next 'nod already existing
Set nod = TreeView1.Nodes.Add(, , NodeName, NodeName)
.Nodes(NodeName).Tag = NodeFullPath
Else
On Error Resume Next 'nod already existing
.Nodes.Add parent, tvwChild, NodeName, NodeName
If .Nodes(NodeName).Tag = "" Then .Nodes(NodeName).Tag = NodeFullPath
End If
Err.Clear
End With
FineEnd:
parent = NodeName
Loop
Next i
For Each nod In Me.TreeView1.Nodes
nod.EnsureVisible
Next nod
'SaveAs Savename
Exit Sub
Riprendi:
Err.Clear
GoTo FineEnd
End Sub
Private Sub CommandButton1_Click()
Call FindTextString
End Sub
Private Sub TreeView1_Click()
On Error Resume Next 'selection is not a node
MsgBox "The full path of " & TreeView1.SelectedItem.Text & " is: " & TreeView1.SelectedItem.Tag
End Sub