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
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,432
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
 

Marbles

Well-known Member
Joined
Feb 7, 2005
Messages
622
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.
 

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,432
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?
 

Marbles

Well-known Member
Joined
Feb 7, 2005
Messages
622

ADVERTISEMENT

Andrew

Yes to both questions.

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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,216
Office Version
  1. 365
Platform
  1. Windows
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'.
 

Marbles

Well-known Member
Joined
Feb 7, 2005
Messages
622

ADVERTISEMENT

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.
 

Marbles

Well-known Member
Joined
Feb 7, 2005
Messages
622
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.
 

Marbles

Well-known Member
Joined
Feb 7, 2005
Messages
622
Sorry about that pompous answer. I should have deleted it.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,371
Messages
5,595,777
Members
414,020
Latest member
Meghdad

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