Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在excel中列出带有链接的文件名_Excel_Directory_Directory Structure_Vba - Fatal编程技术网

在excel中列出带有链接的文件名

在excel中列出带有链接的文件名,excel,directory,directory-structure,vba,Excel,Directory,Directory Structure,Vba,是否有一种方法可以列出excel工作表中不同目录下的文件名以及链接(这样可以单击文件名旁边的链接打开文件) 我已经准备好写脚本了,但我不知道有什么特别的方法或命令 我不想把整件事都打出来 我不关心目录/树结构,但应该存在文件链接 有一个目录包含许多其他文件夹,其中包含我需要列出的文件,大部分是*.pdf 谢谢你的帮助。谢谢 您可以使用下面的代码将主文件夹中的文件名和文件路径设置为strFolderPath 'Global Declaration for Start Row Public l

是否有一种方法可以列出excel工作表中不同目录下的文件名以及链接(这样可以单击文件名旁边的链接打开文件)

  • 我已经准备好写脚本了,但我不知道有什么特别的方法或命令
  • 我不想把整件事都打出来
  • 我不关心目录/树结构,但应该存在文件链接
  • 有一个目录包含许多其他文件夹,其中包含我需要列出的文件,大部分是*.pdf

谢谢你的帮助。谢谢

您可以使用下面的代码将主文件夹中的文件名和文件路径设置为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
我相信你正在寻找这个答案

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 

您无需每次
选择
单元格;您可以使用
Range(单元格(i+1,1)、单元格(i+1,1))
作为锚。此外,这不会处理嵌套文件夹。@ZevSpitz OP post需要一个目录,但为我将来的参考而注明,除了不是我的代码,而是显示两分钟的谷歌将带给你什么:)有一个目录,其中包含许多其他文件夹,其中包含我需要列出的文件。哈!我的糟糕,跳过了“文件夹”这个词,更多的咖啡