5

Could anyone from this group can offer some advice on the problem I'm trying to solve? I was attempting to open a PDF document to a specific page using the Excel Hyperlink function. I tried to solve this mystery but to no avail. Hope someone can provide me their thoughts and ideas.

Excel Table

Main Folder

PDF Reference Folder

I tried Google and Youtube but only few were related to my issue and the solution discussed was not working on my situation... or maybe i missed something. This will be a great help for me once the issue is solved. Thank you in advance.

braX
  • 11,506
  • 5
  • 20
  • 33
Bert
  • 49
  • 1
  • 1
    I don't think that this can be done with a generic formula. Considering the link is recognizable when the `#page=4` is suffixed but it still opens to page 1 tells me you cannot pass parameters like this in Excel like you can in HTML. Even when utilizing an HTML link with the parameter it simply strips it away and begins on page 1. The way I checked this is through Chrome, if you enter `file:///C:/Users/PDF%20Sample%2001.pdf#page=4` in a new tab URL you get brought to that page. Opening the same string in Excel strips that parameter. – Kevin P. Jan 20 '23 at 14:39
  • What browser is Excel opening these links in? As a guess MS Edge might work but other browsers might not. This question is +4 as I write this so please write up your answer if you find one. – user10186832 Jan 20 '23 at 16:58
  • It looks like a VBA solution may exist (but that brings it's own issues...) [Open a specific page of PDF document from Excel hyperlink](https://answers.microsoft.com/en-us/msoffice/forum/all/hyperlink-to-a-page-in-a-pdf-file/8e029292-ad22-46e3-9752-11c6822627ce#:~:text=Open%20object%20by%20clicking%20on,%2D%2D%3EOK%2D%2D%3ESave.) [Opening a PDF to specific page through field reference](https://www.access-programmers.co.uk/forums/threads/opening-a-pdf-to-specific-page-through-field-reference.211370/) – Tragamor Jan 20 '23 at 17:02

1 Answers1

1

Using a macro enabled workbook you can hi-jack the hyperlink open event but it isn't as dynamic as we would hope it to be. Unfortunately, we cannot cancel any followed hyperlink using Worksheet_FollowHyperlink so we need to find a way around following a direct hyperlink.

We can create a hyperlink which goes nowhere by referencing itself then set the text to display as "Follow Link". Because the cell reference is then in the hyperlink, we can use an offset from that to get the desired address and sub address from the column before it; which then allows us to open the PDF at the desired place.

hyperlink settings

Unfortunately, the hyperlink if copied down will still reference the original cell, so each link would need to be edited separately. To date I haven't found a way to use the HYPERLINK function successfully.

The hyperlink format in the cell to the left would need to be the full path appended with "#page=" and the relevant page; e.g. C:\links\blah.pdf#page=6

In the worksheet module

Const AcrobatReader = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.TextToDisplay = "Follow Link" Then
        Dim Ref As String: Ref = Range(Target.SubAddress).Offset(0, -1).Text
        Dim URL() As String: URL = Split(Ref, "#page=")
        If UBound(URL) > 0 Then Call OpenPDFtoPage(URL(0), CLng(URL(1)))
    End If
End Sub

Function OpenPDFtoPage(FilepathPDF As String, page As Long)
    Dim Path As String: Path = AcrobatReader & " /A ""page=" & page & """ " & FilepathPDF
    Shell Path, vbNormalFocus
End Function

The AcroRd32.exe path may need updating for specific installations

Maybe having just one "Follow Link" hyperlink where the URL in the cell next to it is more dynamic may allow this to be a bit more useable.

Tragamor
  • 3,594
  • 3
  • 15
  • 32