PDA

View Full Version : Opening Up a PDF Document via a Command Button



Andy SIBL
11-28-2007, 06:12 AM
I am currently using the following code in an excel sheet:

Private Sub CommandButton51_Click()
Shell ("C:\Program Files\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe C:\FOLDER\myfile.pdf")
End
End Sub

However users with older versions of acrobat or indeed reader 8.0 can not open the document, is there another way of coding this?

Cheers,

Andy

Dr.K
11-28-2007, 08:54 AM
Piece of cake.
Just run Reader from the cmd line through a Shell, and then you don't have to worry about the location of the executables. You can do the same thing with the full version of Acrobat, too.

Note that this is completely unsupported by Adobe, so running the Acrobat.exe from the command line may cause issues. A better (and possibly faster) way to open the full version of Acrobat is to use the Object model, which is what the last sub does.

Hope this helps.

Private Sub OpenPDFReader(PDF_FileName As String)

Dim objShell As Object

Set objShell = CreateObject("WScript.Shell")
objShell.Run ("AcroRd32.exe """ & PDF_FileName & """")
Set objShell = Nothing

End Sub

Private Sub OpenPDFAcrobat(PDF_FileName As String)

Dim objShell As Object

Set objShell = CreateObject("WScript.Shell")
objShell.Run ("Acrobat.exe """ & PDF_FileName & """")
Set objShell = Nothing

End Sub

Private Sub OpenPDFAcrobat2(PDF_FileName As String)

Dim AcroApp As Object
Dim AVDoc As Object

'invoke Acrobat Application and Document
Set AcroApp = CreateObject("AcroExch.App")
Set AVDoc = CreateObject("AcroExch.AVDoc")

'Open file, show Acrobat
AVDoc.Open PDF_FileName, ""
AcroApp.Show

'Clean up objects
Set AVDoc = Nothing
Set AcroApp = Nothing

End Sub

Andy SIBL
11-28-2007, 09:24 AM
Top man, cheers.

Dr.K
11-29-2007, 03:13 PM
Glad to help, let me know if it works out for you.

If this solved your problem, please mark the thread as "Solved".

MWE
11-29-2007, 03:24 PM
I am currently using the following code in an excel sheet:

Private Sub CommandButton51_Click()
Shell ("C:\Program Files\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe C:\FOLDER\myfile.pdf")
End
End Sub

However users with older versions of acrobat or indeed reader 8.0 can not open the document, is there another way of coding this?

Cheers,

Andy Am I missing something here? Why not just programatically hyperlink to the target file and not worry about which version of acrobat or reader is in place. I use this approach in many applications, for example:

Private Sub cmdbtnFetchFile_Click()

Dim strAddress As String

On Error GoTo NoFile
strAddress = "D:\test.pdf"
ActiveWorkbook.FollowHyperlink Address:=strAddress
Exit Sub

NoFile:
MsgBox "Sorry. File is not available at this time", vbInformation, "MA Tools Utilities"

End Sub

MWE
11-29-2007, 03:26 PM
Am I missing something here? Why not just programatically hyperlink to the target file and not worry about which version of acrobat or reader is in place. I use this approach in many applications, for example:

Private Sub cmdbtnFetchFile_Click()

Dim strAddress As String

On Error GoTo NoFile
strAddress = "D:\test.pdf"
ActiveWorkbook.FollowHyperlink Address:=strAddress
Exit Sub

NoFile:
MsgBox "Sorry. File is not available at this time", vbInformation, "MA Tools Utilities"

End Sub
The approach does not care about particular applications. Re Acrobat Reader, this works fine for every version of Acrobat Reader I have ever used (V3 to V8)

Dr.K
11-29-2007, 03:34 PM
Well, I could be wrong about this, but FollowHyperlink relies on the end-user's file associations. For PDF, this could be either Reader or Acrobat.

With the shell method, you can specify which application you want by using "AcroRd32.exe" or "Acrobat.exe"

MWE
11-29-2007, 05:31 PM
Well, I could be wrong about this, but FollowHyperlink relies on the end-user's file associations. For PDF, this could be either Reader or Acrobat.

With the shell method, you can specify which application you want by using "AcroRd32.exe" or "Acrobat.exe"
True, the hyperlink method relies on the end user's file associations. And that is one of the advantages, i.e., the method does not have to worry about where applications are and what application might be the right one. The application that the user normally associates with the particular file type is the one that will activate. Seems like an advantage to me.