使用excel vba宏创建文件夹和文件,并使用树视图和超链接显示

时间:2014-02-07 08:18:52

标签: excel excel-vba treeview vba

我想通过阅读以下路径制作文件夹和文件

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

并在驱动器D:\ like this

下构建文件夹和文件
      project
          tags
              folder2
                   command.txt
          branches
              folder1
                    folder1.1
                           Notes.docx

。然后使用此物理结构键入带有超链接的树视图(请假设我为*具有超链接的名称标记*)使用vba宏在excel表中的最后文件和文件夹中查看。参见

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

所以请为vba noob提供帮助。

1 个答案:

答案 0 :(得分:7)

我认为应该这样做。 此宏将从单元格A1获取文件夹路径,并使用超链接递归列出其内容和子文件夹内容。 更新:已修复,现在正在运行。 :)

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