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
 
It depends how you want to specify which page to open for each link. One idea is to put the page number in the cell below each link and change Worksheet_FollowHyperlink to:

VBA Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Open_PDF_At_Page Target.ScreenTip, Target.Range.Offset(1).Value   
End Sub
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I've found a useful trick for inserting links which point to themselves. Instead of 'Link to:' 'Place in this document', select 'Existing File or Web Page' and enter the Address as ?, i.e. a question mark.

With this, the cell containing the link remains pointing to itself even if you insert or delete rows or columns.
 
Upvote 0
Awesome! works great. ..One thing I notice is that, once a link is used to open the reference document to the respective page, when you try to utilize another link (refrenceing a different page) it will not "jump" to the new page. The refrence document must be closed prior to executing a subsequent link function.
 
Upvote 0
Awesome! works great. ..One thing I notice is that, once a link is used to open the reference document to the respective page, when you try to utilize another link (refrenceing a different page) it will not "jump" to the new page. The refrence document must be closed prior to executing a subsequent link function.
To avoid the user having to close the document, we can call Windows API functions to look for and close an existing PDF window. This assumes that Acrobat Pro/Adobe Reader is set to display PDF documents in separate windows.

This code replaces the previous standard module code.
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    Private Declare PtrSafe Function EnumWindows Lib "user32" (ByVal lpEnumFunc As LongPtr, ByVal lParam As LongPtr) As Long
    Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#Else
    Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If

Private Const WM_CLOSE = &H10

Private Const AcrobatWindowClass = "AcrobatSDIWindow"


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

    Dim PDFexe As String
    Dim AdobeCommand As String
        
    If Dir(PDFfullName) <> vbNullString Then
    
        PDFexe = Get_ExePath(PDFfullName)
        
        AdobeCommand = " /a ""page=" & page & """ "
                
        Find_and_Close_Acrobat_Window Mid(PDFfullName, InStrRev(PDFfullName, "\") + 1)
        
        Shell Chr(34) & PDFexe & Chr(34) & AdobeCommand & Chr(34) & PDFfullName & Chr(34), vbNormal
    
    Else
    
        MsgBox PDFfullName & " doesn't exist", vbExclamation
        
    End If

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


'Close the specified Acrobat Pro/Adobe Reader PDF window by enumerating windows.
'Assumes that Acrobat Pro/Adobe Reader is set to display PDF documents in separate windows.
'Edit > Preferences > General category > untick 'Open documents as new tabs in the same window'

Public Sub Find_and_Close_Acrobat_Window(windowTitle As String)
    EnumWindows AddressOf EnumWindowsCallback, StrPtr(windowTitle)
End Sub


#If VBA7 Then
Public Function EnumWindowsCallback(ByVal hWnd As LongPtr, ByVal findWindowTitle As String) As Long
#Else
Public Function EnumWindowsCallback(ByVal hWnd As Long, ByVal findWindowTitle As String) As Long
#End If

    Dim thisWindowTitle As String, thisWindowClass As String
    Dim stringLen As Long
    
    'Continue enumerating windows by default
    EnumWindowsCallback = 1
    
    thisWindowTitle = Space$(256)
    stringLen = GetWindowText(hWnd, thisWindowTitle, Len(thisWindowTitle))
    thisWindowTitle = Left$(thisWindowTitle, stringLen)
    
    thisWindowClass = Space$(256)
    stringLen = GetClassName(hWnd, thisWindowClass, Len(thisWindowClass))
    thisWindowClass = Left$(thisWindowClass, stringLen)
    
    'Close this window if its title matches the one being sought and stop enumerating windows
    
    If InStr(1, thisWindowTitle, findWindowTitle, vbTextCompare) And thisWindowClass = AcrobatWindowClass Then
        PostMessage hWnd, WM_CLOSE, 0, 0
        EnumWindowsCallback = 0
    End If

End Function
 
Upvote 0

Forum statistics

Threads
1,223,936
Messages
6,175,508
Members
452,650
Latest member
Tinfish

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