Vba 自动超链接提取的文件
我正在使用的代码可以提取pdf文件的整个路径并显示活动工作簿。 但问题是提取的文件没有超链接,即我无法直接单击该单元格打开文件。是否有任何方法可以自动将其超链接,以便单击一次即可直接从excel打开文件 代码如下:Vba 自动超链接提取的文件,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 FileSystemObje
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
打印文件路径后,添加以下内容:
单元格(i+2,13)。选择ActiveCell.Hyperlinks.add ActiveCell,ActiveCell
如果可以,请将“工作表名称”替换为工作表名称:
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
最好避免使用
。在VBA中编写代码时,请选择函数,因为它会降低宏的速度。