【问题标题】:List file names with links in excel在excel中列出带有链接的文件名
【发布时间】:2014-10-13 14:41:51
【问题描述】:

有没有一种方法可以在 Excel 表中列出不同目录中的文件名,并带有链接(因此可以单击文件名旁边的链接并打开文件)?

  • 我准备写一个脚本,但我不知道任何特定的方法或命令。
  • 我不想输入全部内容。
  • 我不关心目录/树结构,但文件链接应该存在。
  • 有一个目录包含许多其他文件夹,其中包含我需要列出的文件,主要是 *.pdf 的。

任何帮助表示赞赏。谢谢!

【问题讨论】:

    标签: excel directory directory-structure vba


    【解决方案1】:

    您可以使用以下代码获取文件名和文件路径将主文件夹设置为 strFolderPath

    'Global Declaration for Start Row
    
    Public lngRow As Long
    
    Sub pReadAllFilesInDirectory()
    
        Dim strFolderPath               As String
        Dim BlnInclude_subfolder        As Boolean
    
        'Set Path here
        strFolderPath = "C:\"
    
        'set start row
        lngRow = 1
    
        'Set this true if you want list of sub-folders as well
        BlnInclude_subfolder = True
    
        '---------- Reading of files in folders and sub-folders------
        Call ListMyFiles(strFolderPath, BlnInclude_subfolder)
        '---------- Reading of files in folders and sub-folders------
    
    End Sub
    
    Sub ListMyFiles(mySourcePath As String, blnIncludeSubfolders As Boolean)
    
        Dim MyObject            As Object
        Dim mySource            As Object
        Dim mySubFolder         As Object
        Dim myfile              As Object
        Dim iCol                As Long
    
        Set MyObject = CreateObject("Scripting.FileSystemObject")
        Set mySource = MyObject.GetFolder(mySourcePath)
    
        'Loop in each file in Folder
        For Each myfile In mySource.Files
    
            iCol = 1
            Sheet1.Cells(lngRow, iCol).Value = myfile.Name  'File Name
            iCol = iCol + 1
            Sheet1.Cells(lngRow, iCol).Value = myfile.Path  'File Path/Location
            lngRow = lngRow + 1
    
        Next
    
        If blnIncludeSubfolders Then
            For Each mySubFolder In mySource.SubFolders
                Call ListMyFiles(mySubFolder.Path, True)
            Next
        End If
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      我相信您正在寻找一个快速的seach 答案。

      Sub Example1()
      Dim objFSO As Object 
      Dim objFolder As Object 
      Dim objFile As Object 
      Dim i As Integer 
      
      'Create an instance of the FileSystemObject 
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      'Get the folder object 
      Set objFolder = objFSO.GetFolder("D:\Stuff\Business\Temp")
      i = 1
      'loops through each file in the directory 
      For Each objFile In objFolder.Files
          'select cell 
          Range(Cells(i + 1, 1), Cells(i + 1, 1)).Select 
          'create hyperlink in selected cell 
          ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _ 
              objFile.Path, _ 
              TextToDisplay:=objFile.Name 
          i = i + 1 
      Next objFile
      End Sub 
      

      【讨论】:

      • 您不需要每次都Select该单元格;您可以使用Range(Cells(i + 1, 1), Cells(i + 1, 1)) 作为锚点。
      • 另外,这不处理嵌套文件夹。
      • @ZevSpitz OP 发布需要一个目录,但要注意我未来的参考,除了不是我的代码,而是显示两分钟的谷歌会带给你什么 :)
      • 有一个目录包含许多其他文件夹,其中包含我需要列出的文件。
      • 哈!我的错,跳过了“文件夹”这个词,更多咖啡吧
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多