Search code examples
google-chromepdfvbscriptacrobat

How to open a Pdf with vbscript in Chrome and search by having a # in the url


Good day,

I am trying to open a pdf url using vbscript. The pdf is on a local pc. I am using vbscript to open the url and using chrome. In my Url I use a #Search=Register this will make the pdf open and search for the first occurrence of Register.

My problem is it seems that my Url gets % encoded even my # gets encoded to %23 and then the chrome pdf preview is not working.

My vbscript code :

Dim strChrome
Dim WShellChrome
Dim strUrl

Set WShellChrome = CreateObject("WScript.Shell")
strChrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
strUrl = "\\myserver/HelpDocumentation/Configuration - Company Configuration.pdf#Search=Register"

WShellChrome.Run chr(34) + strChrome + chr(34) & " " & chr(34) + strUrl + chr(34), 1, false

When I run this code it opens up Chrome with the following url:

file://myserver/HelpDocumentation/Configuration%20-%20Company%20Configuration.pdf%23Search=Register

Note the % url encoding that has occurred. The %20 is a space and the %23 is the #

The browser then cant find the pdf Url because the # was encoded to %23. If I change the %23 in the browser url to # it works and the pdf is displayed.

It seems like the %20 (space) is ok in the url but it does not like the %23(#).

I need the # in the url to open the pdf and search a specific string.

It also seems not to be the vbscript, if I copy the url :

\\myserver/HelpDocumentation/Configuration - Company Configuration.pdf#Search=Register

And paste it into my chrome url it auto converts the spaces to %20 and the # to %23 but the acrobat preview is not working due to the %23 that it thinks is part of the file name.

Hope I make sense please ask if you do not understand.


Solution

  • After replies I tried the following.

    Tried using strUrl = escape(strUrl) but this resulted the strUrl to be fully encoded even the / and \ slashes.

    Then I started checking different Urls as suggested changing my / to \ slashes as well as encoding spaces. This was my result.

    When I use the url withoud encodeing spaces like

    "\\myserver\HelpDocumentation\Configuration - Company Configuration.pdf#Search=Register_"
    

    in chrome it does not encode at all and at every space in the url it opens a new tab. - Not working.

    When I use

    "\\myserver\HelpDocumentation\Configuration%20-%20Company%20Configuration.pdf#Search=Register_"
    

    in chrome it changes to : file://myserver/HelpDocumentation/Configuration%2520-%2520Company%2520Configuration.pdf%23Search=Register_
    Then not working because of double encoding. So it adds a "file:" and also encodes the url again, spaces and # is encoded.

    When I use

    file:\\myserver\HelpDocumentation\Configuration%20-%20Company%20Configuration.pdf#Search=Register_
    

    in chrome it stays the same and in result it is working. So the "file:" appended at the start and encoding just the spaces in the url makes the difference.

    I ended up with the following code. Note the strUrl is a input variable I just hardcoded for example. I also now check if its a pdf then add "file:" as the url can be a file or a report url. This code works.

    Dim strChrome
    Dim WShellChrome
    Dim strUrl
    
    Set WShellChrome = CreateObject("WScript.Shell")
    strChrome = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
    strUrl = "\\myserver\HelpDocumentation\Configuration - Company Configuration.pdf#Search=Register_"
    
    strUrl = Replace(strUrl, " ", "%20")
    
    if(InStr(strUrl,".pdf")) Then
       strUrl = "file:" + strUrl
    End If
    
    WShellChrome.Run strChrome & " " & ""+strUrl+"" , 1, false