【问题标题】:How can I create a hyperlink list directing to a specific page in a pdf file?如何创建指向 pdf 文件中特定页面的超链接列表?
【发布时间】:2022-01-27 07:31:07
【问题描述】:

我有一个很长的 PDF 文件(超过 1000 页的扫描文档)。我想创建一个包含 2000 多个超链接的关键字列表,通过单击页面可以快速将我引导到 PDF 中的某些页面。

例子:

Keyword list

Word   Page(hyperlink)
abc    P560
def    P124    P223
ghi    P49
jkl    P980    P1023   P32
...    ...

到目前为止,我可以在 Chrome 中打开任何页面,如下所示。但是我怎样才能把它们变成列表中的超链接呢?甚至可以在Excel中完成吗?我应该尝试一个 HTML 文件吗?

Sub test()

  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"

  Shell (chromePath & filePath & pageNum)

End Sub

【问题讨论】:

  • 你可以创建一个超链接公式,调用一个函数来调用Shell。

标签: excel vba pdf hyperlink


【解决方案1】:

请尝试下一个方法:

  1. 复制标准模块中的下一个代码,以创建超链接公式(此处位于活动工作表的“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
  1. 在同一个模块中复制下一个函数代码:
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
  1. 运行第一个代码以创建链接,然后单击活动工作表的“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.

【讨论】:

  • @sebastian wei 你没有抽出时间来测试上述解决方案吗?如果经过测试,它没有按您的需要工作吗?
猜你喜欢
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2013-11-28
  • 1970-01-01
相关资源
最近更新 更多