Launch Adobe pdf file from form

AllyBally

New Member
Joined
Mar 6, 2006
Messages
39
I want to open a pdf file using a command button on a form. I have no problem launching a Word document using a module and code, see below, but how do I change this to launch pdfs:

Module:
Sub OpenWordDoc(strDocName As String)

End Sub
Dim objApp As Object

'Opens the document

Set objApp = CreateObject("Word.Application")
objApp.Visible = True
objApp.Documents.Open strDocName
End Sub

Then code on command button:

Private Sub cmdOpenWordDoc_Click()

If IsNull(Me.Filepath) Or Me.Filepath = "" Then
MsgBox "Please Ensure There Is A File Path Associated " & _
"For This Document", _
vbInformation, "Action Cancelled"
Exit Sub
Else
If (Dir(Me.Filepath) = "") Then
MsgBox "Document Does Not Exist In The Specified Location", _
vbExclamation, "Action Cancelled"
Exit Sub
Else

'Opens document in Word
Call OpenWordDoc(Me.Filepath)
End If
End If
End Sub

Thanks in advance
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi

Above your code in your form (not the module) but below any 'Option...' declaration statements, insert the following code:

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

Then change this part of your code:
Code:
 'Opens document in Word
Call OpenWordDoc(Me.Filepath)
to this:
Code:
 'Opens document
Dim retVal as Long
If Right$(Me.Filepath,3) = "doc" Then
  Call OpenWordDoc(Me.Filepath)
Else
  retVal = ShellExecute(hwnd, "open", Me.Filepath, vbNullString, vbNullString, 1)
End If

Please note I haven't added any error checking etc but what this amendment does is automatically open the application associated with the document type (e.g. Adobe for pdf, Excel for xls etc). So you can open any document (not just pdf documents) but if there is an unrecognised document format (e.g. bak) then the ShellExecute line will fail.

HTH, Andrew
 
Upvote 0
Hi CT

I hadn't see that thread. At least we both provided a very similar answer!

Cheers
Andrew
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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