• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Open PDF file using hyperlink

YasserKhalil

Well-Known Member
Hello everyone
I am trying to use this simple code to open PDF file
Code:
Sub TestIt()
    ActiveWorkbook.FollowHyperlink ThisWorkbook.Path & "\Sample.pdf"
End Sub

I put Sample.pdf file in the same path of this workbook of course
I got a message like that
01.png
after clicking ok I got an error message
02.png
I installed adobe reader in this path

I have another working code
Code:
Sub RunPDF()
    Dim myPath As String, myFile As String
    myPath = "C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"
    myFile = ThisWorkbook.Path & "\Sample.pdf"
    Shell myPath & " " & myFile, vbNormalFocus
End Sub

But I need to know the reason for the message error in the first code
Thanks advanced for help
 
Personally, I would use ShellExecute rather than hardcoding the path to the executable:
Code:
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
Const SW_HIDE            As Long = 0&
Const SW_SHOW            As Long = 5&

Sub OpenFile(strFilePath As String)
   ShellExecute Application.hWnd, "Open", strFilePath, 0&, 0&, SW_SHOW
End Sub

Then you can open any file type by simply passing the path to the OpenFile routine.
 
Last edited:
Thanks Mr. Debaser for quick reply
As for pdf file .. yes it is working in Explorer

I tested your code as following
Code:
Sub TestRun()
    OpenFile ThisWorkbook.Path & "\Sample.pdf"
End Sub
But nothing happened

Is it possible that the fault is that I installed Adobe Acrobat X Professional ?
I doubt in that possbility
 
No, if it works through Explorer it should work through ShellExecute as they basically use the same registry entries. Did you already have Acrobat open when you tested the code and, if so, are you sure it didn't open as a new tab?
 
Mr. Debaser
When testing the code I didn't open any acrobat files at all
Mr. Veltem
The same problem with me in the first post .. not program is registered to open this file
 
That means you need to set default program to open PDF.

Right click on any PDF file and go to "Open With"->"Choose default program..."
upload_2016-4-25_17-0-22.png
Either pick Acrobat Reader DC or some other program that's compatible.
 
Yes Mr. Chihiro
You are completely right .. and my doubts were right .. It is because I installed Acrobat Pro after standard so I had to choose default program .. although it was opened directly without asking
Now everything is ok
Thanks a lot for great idea
 
Back
Top