Open a PDF from Excel with VBA in Google Chrome on a specific page
I am creating a macro in Excel that should open a PDF document on a specified page with chrome. Generally, the opening part works fine. My only problem is that when I add the page number (e.g. #page=15) to the url, the shell somehow encodes the "#" symbol into "%23", which chrome is not able to interpret correctly anymore (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
I would really appreciate it if anyone knew a solution to this. Maybe there's a different way to do this with Chrome, but I haven't found anything that reads the installation path dynamically. Sry for the probably messy code, I'm new to this.
If Adobe Acrobat Reader is installed on the system I would suggest to use the funcion
openPDFfrom 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
You just need to specify the protocol
file:///if you want to load files from the local hard disk. Then
#gets not translated into
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""file:///C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""")