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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,450
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
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
625
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,450
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
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
625

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,351
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
625

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
625
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
625
Sorry about that pompous answer. I should have deleted it.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,024
Messages
5,856,883
Members
431,837
Latest member
megantang

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