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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Tarkan VURAL

New Member
Joined
Oct 28, 2008
Messages
7
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
 

tinkythomas

Active Member
Joined
Dec 13, 2006
Messages
432
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
 

tinkythomas

Active Member
Joined
Dec 13, 2006
Messages
432
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:

JP2112

Board Regular
Joined
Oct 27, 2008
Messages
237

ADVERTISEMENT

Try this:

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

--JP
 

tinkythomas

Active Member
Joined
Dec 13, 2006
Messages
432
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.
 

JP2112

Board Regular
Joined
Oct 27, 2008
Messages
237

ADVERTISEMENT

That's usually the cause when the usual methods of opening PDFs stop working. Glad to hear it worked out!

--JP
 

Tarkan VURAL

New Member
Joined
Oct 28, 2008
Messages
7
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
 

tinkythomas

Active Member
Joined
Dec 13, 2006
Messages
432
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
 

El Tio

New Member
Joined
Nov 11, 2008
Messages
1
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?
 

Forum statistics

Threads
1,141,203
Messages
5,704,934
Members
421,372
Latest member
Jamie11

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
Top