#1
|
|||
|
|||
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 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 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! |
#2
|
||||
|
||||
I don't use either of these PDF readers, but whichever you call you have to use the syntax it understands and not that for Adobe Reader.
See https://github.com/sumatrapdfreader/...line-arguments
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thank you so much gmayor. With your help, I was able to make it work:
Code:
Sub GoToPDFhyperlinkPage() 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 = Chr(34) & "C:\Program Files\SumatraPDF\SumatraPDF.exe" & Chr(34) ' need it enclosed in quotes b/c there is a space in "Program Files" RtnCode = Shell(AdobePath & " " & Chr(45) & "page" & " " & iMyPageNumber & " " & Chr(45) & "reuse" & Chr(45) & "instance" & " " & Chr(34) & sMyPDFPath & Chr(34), 1) End Function |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro: How to get this macro to save to a specific location | LOUF | Word VBA | 1 | 12-07-2015 06:47 PM |
Macro for word to add page break and specific text to end of document | pizzaman1 | Word VBA | 6 | 11-14-2014 11:25 PM |
Macro Needed to bold specific lines and Macro to turn into CSV | anewteacher | Word VBA | 1 | 05-28-2014 03:59 PM |
can I run a macro when I open a specific doc. | shreked | Word | 8 | 01-12-2012 03:36 AM |
Why isn't my hyperlinked word text hyperlinked when I convert it to pdf? | Obisco | Word | 0 | 12-10-2010 01:58 PM |