Open pdf file from excel vba?

tinkythomas

Active Member
Joined
Dec 13, 2006
Messages
432
I'm trying to open a pdf file from within excel vba. I have tried using the followhyperlink method but adobe acrobat opens very briefly then immediately closes:confused:
Code:
Sub OpenPDF()

'Dim pdf As String

    On Error Resume Next

    'pdf file to open
    pdf = "K:\PDF\mypdf.pdf"

    'open the pdf file
    ActiveWorkbook.FollowHyperlink pdf

End Sub
So then I tried to create an instance of acrobat by setting a reference to the acrobat object but I can't get this to work either!

The code I'm using is
Code:
Sub OpenPDF()

    Dim pdf As AcroPDDoc
    Dim strPDF As String

    Set pdf = CreateObject("AcroExch.PDDoc")
    
    'pdf file to open
    strPDF = "K:\PDF\mypdf.pdf"

    'open the pdf file
    pdf.Open strPDF

End Sub
Any ideas what could be wrong with either approach?

Thank-you
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi tinkythomas ;

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

Code:
Sub EVN()
Filename = Application.DefaultFilePath & "\myPDF.Pdf"
ShellExecute 0, "Open", Filename, "", "", vbNormalNoFocus
End Sub
 
Upvote 0
Hi Tarkan, where do I place the first line sof code? I'm receiving a compile error with the code as displayed. The error is "Only comments may appear after End Sub, End Function or End Property"

Sorry my vba skills are still poor!

Thanks
 
Upvote 0
Ok, I have now placed the code in a new module and have eliminated the compile error! But the pdf file doesn't open. I have systernals process explorer to monitor whether acobat.exe launches and it does but then immediately closes:confused:

Any ideas??
 
Last edited:
Upvote 0
Try this:

Code:
Dim myShell As Object
Set myShell =  CreateObject("WScript.Shell")
myShell.Run "K:\PDF\mypdf.pdf"

--JP
 
Upvote 0
Thanks JP, it appeared that my acrobat pro install was corrupt! Although I could open pdf files normally by double clicking:confused:

Anyway, I uninstalled acrobat pro and installed the latest reader and now I can open pdf's using the followhyperlink method.

Not sure what was wrong with the pro install, maybe I will re-install and have another go? But for now with the reader all appears to be well.

Thanks for evryone's help it has been a great help.
 
Upvote 0
That's usually the cause when the usual methods of opening PDFs stop working. Glad to hear it worked out!

--JP
 
Upvote 0
Hi again,
Alternative code example
Code:
Sub RunPDFWithExe()
Shell "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe C:\Documents and Settings\user\Desktop\Traveller_ypk_full.pdf", vbNormalFocus
End Sub

or

Code:
Sub RunPDFWithExe()
MyPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"
MyFile = "C:\Documents and Settings\user\Desktop\Traveller_ypk_full.pdf"
Shell MyPath & " " & MyFile, vbNormalFocus
End Sub
Please change this Mypath and MyFile ...


or look at this example movie :rolleyes:
http://www.excelvba.net/forum/RunPdf.html
 
Upvote 0
Tarkan VURAL, thank-you for the reply. I'm sorry it's taken so long for me to reply, have been very busy recently.

Thanks for the revised code, this works perfectly. I now have several methods for opening pdf files.

Thanks
 
Upvote 0
What command would you use for doing something similar with PDF but printing instead of opening? I have a very similar application to tinkythomas but printing is my objective, not opening. Thoughts?
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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