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. 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.

Upvotes: 2

Views: 4506

Answers (2)

Storax
Storax

Reputation: 12167

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

Upvotes: 2

Pᴇʜ
Pᴇʜ

Reputation: 57683

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""") 

Upvotes: 3

Related Questions