自动超链接提取的文件

时间:2016-03-24 12:54:40

标签: vba excel-vba excel

我正在使用一个代码,该代码可以提取pdf文件的整个路径并显示活动工作簿。 但问题是提取的文件没有超链接,即我无法直接点击该单元格打开文件。是否有任何方式可以自动进行超链接,以便一次单击直接从excel打开文件。

以下是代码:

Sub ReadFiles()
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(Range("C1").Value)

i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
If Right(objFile.Path, 3) = "pdf" Then
   'print file path
    Cells(i + 2, 13) = objFile.Path
    i = i + 1
End If
Next objFile
End Sub

2 个答案:

答案 0 :(得分:0)

打印文件路径后,添加以下内容:cells(i + 2,13).select ActiveCell.Hyperlinks.Add ActiveCell, ActiveCell

答案 1 :(得分:0)

这应该有效,将“WorksheetName”替换为工作表的名称:

Sub ReadFiles()
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(Range("C1").Value)

i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
If Right(objFile.Path, 3) = "pdf" Then
   'print file path
    Cells(i + 2, 13) = objFile.Path
    Sheets("WorksheetName").Hyperlinks.Add _
    Anchor:= Sheets("WorksheetName").Cells(i + 2, 13), _
    Address:= objFile.Path
    i = i + 1
End If
Next objFile
End Sub