Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-10-2016, 09:27 PM
poetofpiano poetofpiano is offline Macro to Go To Specific Page in Hyperlinked PDF Windows 8 Macro to Go To Specific Page in Hyperlinked PDF Office 2013
Novice
Macro to Go To Specific Page in Hyperlinked PDF
 
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
  #2  
Old 12-11-2016, 02:59 AM
gmayor's Avatar
gmayor gmayor is offline Macro to Go To Specific Page in Hyperlinked PDF Windows 10 Macro to Go To Specific Page in Hyperlinked PDF Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 12-11-2016, 04:59 PM
poetofpiano poetofpiano is offline Macro to Go To Specific Page in Hyperlinked PDF Windows 8 Macro to Go To Specific Page in Hyperlinked PDF Office 2013
Novice
Macro to Go To Specific Page in Hyperlinked PDF
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to Go To Specific Page in Hyperlinked PDF Macro: How to get this macro to save to a specific location LOUF Word VBA 1 12-07-2015 06:47 PM
Macro to Go To Specific Page in Hyperlinked PDF 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft