请尝试下一个方法:
- 复制标准模块中的下一个代码,以创建超链接公式(此处位于活动工作表的“A1”中):
'Hyperlink function to call macro____________________________________
Sub testCreateHyperlinkFunction()
Dim chromePath, filePath, pageNum As String
chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
filePath = "file:\\\C:\Users\Sebastian\Documents\textbook.pdf""
pageNum = "#page=2"
'Very important to have # in front of the function name!
Range("A1").Formula = "=HYPERLINK(""#OpenPdfPageClick()"", """ & chromePath & "|" & filePath & pageNum & """)"
End Sub
- 在同一个模块中复制下一个函数代码:
Function OpenPdfPageClick()
Set OpenPdfPageClick = Selection
Dim arr: arr = Split(Selection.Value, "|")
Shell Chr(34) & arr(0) & Chr(34) & " " & Chr(34) & arr(1) & Chr(34)
End Function
- 运行第一个代码以创建链接,然后单击活动工作表的“A1”单元格,您可以在其中看到创建的链接。
可以从具有不同pdf文件全名的单元格中调用相同的函数。我尝试展示一种向函数提供 Chrome 完整路径的方法,但它们都由 Chrome 运行,它的全名可以放在函数代码内的 Constant 中,并且只传递 pdf 全名。
由于 Chrome 安装文件夹可能因用户而异,因此还可以实现从注册表中提取它的功能。其实我也会贴出这样一个功能:
Function getChromeExePath(strExeApp As String) As String
Dim WSHShell As Object: Set WSHShell = CreateObject("WScript.Shell")
getChromeExePath = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" & strExeApp & "\")
End Function
可以在上面调用Sub,创建链接的方式如下:
Instead of:
chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
you can use:
chromePath = getChromeExePath("chrome.exe")
Test it and send some feedback, please. I tested it (with one of my files) and it works as is should.