Open a PDF from Excel with VBA in Google Chrome on a specific page

喜夏-厌秋 提交于 2021-01-24 08:30:52

问题


I am creating a macro in Excel that should open a PDF document on a specified page with chrome.

Generally, the opening part works. My problem is that when I add the page number (e.g. #page=15) to the url, the shell encodes the "#" symbol into "%23", which Chrome is not able to interpret correctly (file not found).

Here is my code

'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

Is there a different way to do this with Chrome? I haven't found anything that reads the installation path dynamically.


回答1:


You just need to specify the protocol file:/// if you want to load files from the local hard disk. Then # gets not translated into %23.

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



回答2:


If Adobe Acrobat Reader is installed on the system I would suggest to use the funcion openPDF from Daniel Pineault. This will open the file in Adobe Reader directly. You can find the source code of the function here

A test could look like that

Sub TestSO()

Dim fileName As String
Dim pageNo As Long

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

End Sub


来源:https://stackoverflow.com/questions/52830555/open-a-pdf-from-excel-with-vba-in-google-chrome-on-a-specific-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!