how to open specific page in pdf

bhandari

Active Member
Joined
Oct 17, 2017
Messages
359
iam looking for a formula or vba in excel which will be easy to access to open specific page in pdf

i have 64 bit adobe Acrobat professional 8
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this, changing the Acrobat.exe (or Adobe Reader) path and PDF file name as required.
Code:
Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe"

Public Sub Open_PDF_At_Page()

    Dim PDFfile As String
    Dim AdobeCommand As String
    
    PDFfile = "C:\path\to\PDF document.pdf"
    
    AdobeCommand = " /a ""page=2=Open Actions"" "
    
    Shell cAdobeReaderExe & AdobeCommand & Chr(34) & PDFfile & Chr(34), vbNormal

End Sub
 
Upvote 0
Its Working fine but every time i have to change page number in code

is it possible to get it by using cell reference or function because lot of page links are there for individual cells?

i have to give cell reference to each cell
Ex:
A1=10

10th page need to open
 
Upvote 0
Try this, changing the Acrobat.exe (or Adobe Reader) path and PDF file name as required.
Code:
Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe"

Public Sub Open_PDF_At_Page()

    Dim PDFfile As String
    Dim AdobeCommand As String
    
    PDFfile = "C:\path\to\PDF document.pdf"
    
    AdobeCommand = " /a ""page=2=Open Actions"" "
    
    Shell cAdobeReaderExe & AdobeCommand & Chr(34) & PDFfile & Chr(34), vbNormal

End Sub
Hi John, I was trying to use this code with my Mac. But its not working there. Any suggestions ?
 
Upvote 0
Try this, changing the Acrobat.exe (or Adobe Reader) path and PDF file name as required.
Code:
Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe"

Public Sub Open_PDF_At_Page()

    Dim PDFfile As String
    Dim AdobeCommand As String
   
    PDFfile = "C:\path\to\PDF document.pdf"
   
    AdobeCommand = " /a ""page=2=Open Actions"" "
   
    Shell cAdobeReaderExe & AdobeCommand & Chr(34) & PDFfile & Chr(34), vbNormal

End Sub

Hello John - New to this website so forgive me if I'm doing something incorrectly... This seems to be real close to what I am trying to do, but when I run the program I get a run-time error '5' invalid procedure call or argument...
I'm currently using Adobe DC (2015) and would like to open a specific page in from a PDF refrence document via a link in excel (Office 365). I would certainly appreicate any help you could give..
Thx,
 

Attachments

  • Code capture.PNG
    Code capture.PNG
    75.8 KB · Views: 93
Upvote 0
The error suggests a problem with the Shell statement. Try adding quotes around the exe, like this:
Code:
    Shell Chr(34) & cAdobeReaderExe & Chr(34) & AdobeCommand & Chr(34) & PDFfile & Chr(34), vbNormal
I'm currently using Adobe DC (2015) and would like to open a specific page in from a PDF refrence document via a link in excel (Office 365). I would certainly appreicate any help you could give..
Thx,
This can be done with the Worksheet_FollowHyperlink event handler, but with the link created in a particular way which allows the code to override the default action of clicking a link. In this example, create the link in cell A7 with the following properties:

Place in this document
Text to display: anything you want, e.g. PDF file name
Cell reference: A7, i.e. the link refers to the cell it is located in
Screentip: full file name of PDF file including path

Add this code to a new standard module:
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
    Private Declare PtrSafe Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
       (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
#Else
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
       (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
#End If


Public Sub Open_PDF_At_Page(PDFfile As String, Optional page As String = "1")

    Dim PDFexe As String
    Dim AdobeCommand As String
        
    PDFexe = Get_ExePath(PDFfile)
    
    AdobeCommand = " /a ""page=" & page & "=Open Actions"" "
    
    Shell Chr(34) & PDFexe & Chr(34) & AdobeCommand & Chr(34) & PDFfile & Chr(34), vbNormal

End Sub


Private Function Get_ExePath(lpFile As String) As String
    Dim lpDirectory As String, sExePath As String, rc As Long
    lpDirectory = "\"
    sExePath = Space(255)
    rc = FindExecutable(lpFile, lpDirectory, sExePath)
    Get_ExePath = Left$(sExePath, InStr(sExePath, Chr$(0)) - 1)
End Function
Note that you don't need to specify the Adobe exe path, because the above code determines the default .exe for opening PDF files.

Right-click the sheet tab of the A7 cell and click View Code. Put this code in the sheet module:

Code:
Option Explicit

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)   
    Open_PDF_At_Page Target.ScreenTip, Range("A10").Value
End Sub
Cell A10 contains the page number to open the PDF at. If empty the PDF is opened at page 1.
 
Upvote 0
Thanks John - Doesn't seem to like the Shell statement again...
Run time error '5' - invalid procedure call or argument without quotes around exe (PDFexe)
Run-time error '53' - file not found with quotes around exe ("PDFexe")
 
Upvote 0
Thanks John - Doesn't seem to like the Shell statement again...
Run time error '5' - invalid procedure call or argument without quotes around exe (PDFexe)
Run-time error '53' - file not found with quotes around exe ("PDFexe")

"PDFexe" is definitely wrong, because PDFexe is a string variable, not a string literal. Chr(34) & PDFexe & Chr(34) puts quotes around the .exe.

Does the PDF file exist? I can reproduce Run-time error '5' if the PDF doesn't exist. To check for a non-existent file, change the Open_PDF_At_Page routine to:

VBA Code:
Public Sub Open_PDF_At_Page(PDFfile As String, Optional page As String = "1")

    Dim PDFexe As String
    Dim AdobeCommand As String
        
    If Dir(PDFfile) <> vbNullString Then
    
        PDFexe = Get_ExePath(PDFfile)
        
        AdobeCommand = " /a ""page=" & page & "=Open Actions"" "
        
        Shell Chr(34) & PDFexe & Chr(34) & AdobeCommand & Chr(34) & PDFfile & Chr(34), vbNormal
    
    Else
    
        MsgBox PDFfile & " doesn't exist", vbExclamation
        
    End If

End Sub
 
Upvote 0
Got it. Thank you! I had a security setting issue with Adobe.. As an aside, how would I go about having multiple links of this kind within one worksheet?
 
Upvote 0

Forum statistics

Threads
1,223,935
Messages
6,175,494
Members
452,649
Latest member
mr_bhavesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top