Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

treeview - Make folders and files using excel vba macro and display with tree view and hyperlinks

I would like to make folder and file by reading the following paths

       /project/tags/folder2/command.txt
       /project/branches/folder1/folder1.1/Notes.docx

and construct folders and files under drive D: likes this

      project
          tags
              folder2
                   command.txt
          branches
              folder1
                    folder1.1
                           Notes.docx

.Then use this physical structure to type tree view with hyperlinks(Please assume I mark * for the names that words have hyperlinks) at last files and folders in excel sheet using vba macro.See

      project
         |_tags
         |   |_folder2*
         |         |_command.txt*
         |_branches
         |     |_folder1
         |           |_folder1.1*
         |                 |_Notes.docx*

So please help something for vba noob.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think that should do the trick. This macro will take folder path from cell A1 and list recursively its contents and subfolder contents with hyperlinks. Update: fixed, now it's working. :)

Public Position As Integer
Public Indent As Integer

Sub ListFileTree()

Position = 0
Indent = 0

Call RecurseFolderList(Range("A1").Value)

End Sub

Private Sub ClearFormatting(Rng As Range)

    Rng.Formula = Rng.Value2
    Rng.Font.ColorIndex = xlAutomatic
    Rng.Font.Underline = xlUnderlineStyleNone

End Sub

Function GetFilenameFromPath(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function

Function RecurseFolderList(FolderName As String) As Boolean
    On Error Resume Next
    Dim FSO, NextFolder, FolderArray, FileArray, NextFile
    Dim OriginalRange As Range
    Dim RemoveHyperlink As Boolean
    Set FSO = CreateObject("Scripting.FileSystemObject")

    If Err.Number > 0 Then
        RecurseFolderList = False
    Exit Function

    End If

    On Error GoTo 0
    If FSO.FolderExists(FolderName) Then

        Set NextFolder = FSO.GetFolder(FolderName)
        Set FolderArray = NextFolder.SubFolders
        Set FileArray = NextFolder.Files

        RemoveHyperlink = False
        Set OriginalRange = Range("A2").Offset(Position - 1, Indent)

        Indent = Indent + 1

        For Each NextFolder In FolderArray

            Range("A2").Offset(Position, Indent).Formula = "=HYPERLINK(""" & NextFile & """,""" & UCase(GetFilenameFromPath(NextFolder)) & """)"
            Position = Position + 1

            RecurseFolderList (NextFolder)

            RemoveHyperlink = True
        Next

        For Each NextFile In FileArray

            Range("A2").Offset(Position, Indent).Formula = "=HYPERLINK(""" & NextFile & """,""" & GetFilenameFromPath(NextFile) & """)"
            Position = Position + 1

            RemoveHyperlink = False

            DoEvents
        Next

        If RemoveHyperlink Then
            Call ClearFormatting(OriginalRange)
        End If

        Set NextFolder = Nothing
        Set FolderArray = Nothing
        Set FileArray = Nothing
        Set NextFile = Nothing

    Else
        RecurseFolderList = False
    End If

    Set FSO = Nothing
    Indent = Indent - 1

End Function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...