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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi

You don't need to use early binding to open these types of documents. By using a shell command, VBA will open the programme assigned to the document type. The following code will open any document that is entered into the textbox called 'FilePath':

Code:
Option Compare Database
Option Explicit

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

Const SW_SHOWMAXIMIZED = 3

Private Sub cmdOpenDoc_Click()

Dim myDoc As String
myDoc = Trim(Me.Filepath.Value)

If Len(myDoc) > 0 Then
    If Dir(myDoc) = "" Then
        MsgBox "That document does not exist", vbCritical, "Error"
    Else
        Call ShellExecute(Me.hwnd, "open", myDoc, "", 0, SW_SHOWMAXIMIZED) 
    End If
End If

End Sub

HTH, Andrew
 
Upvote 0
Andrew

I don't suppose that would work with a mail merge document based on the database. Would the word file try and open the database again?

I haven't tried, but it would be a good shortcut if it worked.
 
Upvote 0
Hi Marbles

I can honestly say I have never used a mail merge between Word and Access because I'm happy enough with the report layouts per Access. Do you want to give it a test and post back with the results?

Andrew

P.S. Is that another movie avatar?
 
Upvote 0
Andrew

Yes to both questions.

This film has one of the funniest football games ever. In an otherwise bleak film.
 
Upvote 0
Marbles

Is that from Kes?

If it is I wonder whatever happened to the young guy that played the main character - perhaps time for another perusal of the IMDb.

I do recall some of the other characters/directors went on to become 'famous'.
 
Upvote 0
Andrew

Yes, Kes. He blamed Kes for blighting his career. It was such a well renowned film, and his part so burnt into the memory, no one could think what do next.
 
Upvote 0
The adults were played by "Real Actors", but most of the children were amateurs. Ken Loach liked, what he thought was working class realism. Being working class myself, I realised that he only caught what he could see. His films took a long time to show any tenderness.
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,291
Members
448,564
Latest member
ED38

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