【问题标题】:Open a PDF from Excel with VBA in Google Chrome on a specific page在特定页面上的 Google Chrome 中使用 VBA 从 Excel 打开 PDF
【发布时间】:2019-03-20 16:43:58
【问题描述】:

我正在 Excel 中创建一个宏,它应该使用 chrome 在指定页面上打开 PDF 文档。

通常,开头部分有效。我的问题是,当我将页码(例如 #page=15)添加到 url 时,shell 将“#”符号编码为“%23”,Chrome 无法正确解释(找不到文件)。

这是我的代码

'The path to the file, replaces spaces with the encoding "%20"
Path = Replace((filePath& "#Page=" & iPageNum), " ", "%20")

Dim wshShell, chromePath As String, shellPath As String
Set wshShell = CreateObject("WScript.Shell")
chromePath = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe\")

shellPath = CStr(chromePath) & " -url " & Path

If Not chromePath = "" Then
    'how I first tried it:
    Shell (shellPath) 

    'for testing purposes, led to the same result though:
    Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""") 

End If

Chrome 是否有其他方法可以做到这一点?我还没有找到任何动态读取安装路径的东西。

【问题讨论】:

  • 是否也可以使用 Adob​​e Acrobat Reader 代替?

标签: excel vba google-chrome pdf


【解决方案1】:

如果系统上安装了 Adob​​e Acrobat Reader,我建议使用 Daniel Pineault 的函数openPDF。这将直接在 Adob​​e Reader 中打开文件。可以找到函数here的源码

测试可能是这样的

Sub TestSO()

Dim fileName As String
Dim pageNo As Long

    fileName = "Path and filename of PDF"
    pageNo = 20
    OpenPDF fileName, 20

End Sub

【讨论】:

  • “如果系统上安装了 Adob​​e Acrobat Reader,我假设因为它必须是这样,否则 Chrome 的解决方案也会失败” 为什么会失败? Chrome 有一个内置的 PDF 查看器,在默认模式下根本不使用 Acrobat Reader。
  • 啊,对不起,我不知道 Chrome 有一个内置的 PDF 查看器。我只是假设它使用 Adob​​e。我会更正我的帖子。
  • 在任何情况下都有用 :-) @Storax
【解决方案2】:

如果你想从本地硬盘加载文件,你只需要指定协议file:///。然后# 不会被翻译成%23

 Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""file:///C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""") 

【讨论】:

  • 谢谢!如此简单的修复,它就像一个魅力:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2011-05-19
相关资源
最近更新 更多