Business Software/Opening a PDF file from a command button
Expert: Todd Binenstock - 10/14/2007
QuestionHi Todd
I have Excel 2003 and I am wanting to open a .pdf file using a command button. I have found that
Shell ("C:\Program Files\Adobe\Acrobat 7.0\Acrobat\acrobat.exe C:\Program Files\PERFORM\instuctions.pdf"), 3
Works ok. But the problem I have is that the command to find acrobat.exe is too specific. I tried to copy the acrobat.exe into the same file as the speardsheet but it refused to open it. I don't want to have to rewrite the VB every time I change the version of Acrobat. Is there a way round this problem.
Regards
Eric
AnswerThis took a little research and experimentation. I found this helpful info at:
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvb4/html/ms...
I will simplify for you.
The "obvious" solution is to use the fact that the pdf file extension is associated in windows explorer with the current version of acrobat installed. Thus we can use that if we have the following code calling functions from the shell32.dll in your excel vba module:
'This declaration must be at the top
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
'This code can be anywhere, probably in the same module
Public Sub OpenThisDoc(FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(FormName, "Open", FileName, 0&, 0&, SW_SHOWMAXIMIZED)
End Sub
'this is the one you would run
Sub Run_me()
OpenThisDoc FileName:="c:\a.pdf" 'put the path and filename to your pdf file
End Sub
Let me know how this works for you.