View Single Post
 
Old 12-10-2016, 09:27 PM
poetofpiano poetofpiano is offline Windows 8 Office 2013
Novice
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default Macro to Go To Specific Page in Hyperlinked PDF

I am trying to create a Word 2016 VBA macro that goes to a specific page in a hyperlinked PDF when the page number is part of the hyperlink address.

The most I could find on the subject is at https://stackoverflow.com/questions/...from-word-2013. The solution offered there is not of interest to me because I need the PDF to open with either FoxitPDF reader or SumatraPDF—not an internet browser. But one user at that link offered the following VBA macro solution:

Code:
Sub [Name Of Script]()
Dim targetLink As String
Dim targetName As String
Dim pageNumber As Integer
Dim pathPDF As String
targetName = Selection.Hyperlinks(1).Name
parts = Split(targetName, "page=")
pageNumber = parts(1)
pathPDF = Selection.Hyperlinks(1).Address
Call OpenPagePDF(pathPDF, pageNumber)
End Sub

Public Function OpenPagePDF(sMyPDFPath As String, iMyPageNumber As Integer)
Dim RtnCode, AdobePath As String
AdobePath = "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"
RtnCode = Shell(AdobePath & " /a " & Chr(34) & "page=" & iMyPageNumber & "=OpenActions" & Chr(34) & " " & Chr(34) & sMyPDFPath & Chr(34), 1)
End Function
This solution uses Adobe reader to open a word hyperlink to a PDF and assumes that the hyperlink address is named something like C:\Users\John\Dropbox\Examplefile.pdf#page=5 so that the macro knows to go to page 5 of the PDF. I tried to modify this macro to point to Sumatra as follows:

Code:
Sub [Name Of Script]()
Dim targetLink As String
Dim targetName As String
Dim pageNumber As Integer
Dim pathPDF As String
targetName = Selection.Hyperlinks(1).Name
parts = Split(targetName, "page=")
pageNumber = parts(1)
pathPDF = Selection.Hyperlinks(1).Address
Call OpenPagePDF(pathPDF, pageNumber)
End Sub

Public Function OpenPagePDF(sMyPDFPath As String, iMyPageNumber As Integer)
Dim RtnCode, AdobePath As String
AdobePath = " C:\Program Files\SumatraPDF\SumatraPDF.exe"
RtnCode = Shell(AdobePath & " /a " & Chr(34) & "page=" & iMyPageNumber & "=OpenActions" & Chr(34) & " " & Chr(34) & sMyPDFPath & Chr(34), 1)
End Function
However, it does not open the desired PDF at page 5. Instead it opens the PDF in Sumatra at page 1 as well as two other Sumatra windows: one that says “error loading C:\a” and another that says “Error loading C:\Users\John \Dropbox\page=5=OpenActions”

I thought maybe this was just a problem with Sumatra PDF, but when I change the executable to point to Foxit, the desired PDF doesn’t even open at all when I execute the macro — Foxit just opens with a blank background.

Does anyone have any ideas on how to make this a working macro? Thank you so much in advance!
Reply With Quote