Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 34 of 34

Thread: A challenge for someone

  1. #21
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thats pretty cool, and was where i was trying to get, have you considered making this a kb entry as i am sure others would benifit from this as much as I have.

    One final question ( I think )

    I can use MsgBox TreeView1.SelectedItem to show the item selected, is there a way of showing the parent nodes (If there are any) as well?

  2. #22
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    that's very easy. Set the parent variable (you find it in the code) as the tag of each nodes (do it before you update the variable with the new node). Then you can get the parents names using their tags.

    About the entry, I agree with you. But first code must be cleared. I'm going to do it next days.
    I will record you and me as the authors.

  3. #23
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Of course if you need the path of all the parent folders, create a string that is the sum of all the parent variable you have while macro is running.

  4. #24
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    that's very easy. Set the parent variable (you find it in the code) as the tag of each nodes (do it before you update the variable with the new node). Then you can get the parents names using their tags.
    Easy when you know how, but you ll have to explain to me in more detail please

    Cheers

    Gibbo

  5. #25
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Anyone?

  6. #26
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Ok, first you'll explain it to me. Do you need, for any node, a list of all the parent folders or the path of the parent folder?

  7. #27
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    try this:


    Private Sub TreeView1_Click()
    On Error Resume Next 'selection is not a node
    MsgBox "The parent folder of " & TreeView1.SelectedItem.Text & " is " _ 
    & TreeView1.SelectedItem.parent.Text
    End Sub

  8. #28
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thanks ALe, i wont be able to try it till this evening but im sure it will do what im after

    Cheers

    Gibbo

  9. #29
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Ok, let me know

  10. #30
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    ALi, i need the full path not just the parent folder if thats possible?

  11. #31
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    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

  12. #32
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    That was perfect

    Cheers

    Gibbo

  13. #33
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Ok. Of course now the code has some lines useless. Remove them if you want to speed up the procedure.

    Bye, ALe

  14. #34
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thanks ALe

    Can i remove more from this code than I have now then?

    Sub FindTextString() 
        Dim i As Integer 
        Dim szSearchWord As Variant 
        Dim sFollder As String 
        Dim nod As Node 
        Dim MyPath, NodeName, parent As String 
        Dim n, k As Integer 
        Dim NodeFullPath As String 
    sFollder = GetFolderPath 
        If sFollder <> vbNullString Then MsgBox sFollder 
    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." 
    End With 
        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
    Cheers

    Gibbo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •