I can open Excel and Word Documents from the following routine but I don't know how to open a pdf file. Any help?
Sub OpenFile()
Dim fn As Variant
ChDir "C:\Program Files\FolderName"
fn = Application.GetOpenFilename("All files,*.*,Excel-files,*.xls,Word Files,*.doc,PDF Files,*.pdf,", _
1, "Folder Name- Select folder and file to open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
' the user didn't select a file
Debug.Print "Selected file: " & fn
Select Case Right(fn, 3)
Case Is = "xls"
Workbooks.Open fn
Case Is = "doc"
Call GetWord(CStr(fn))
Case Else
MsgBox "How did you select a non xls or doc file?"
End Select
End Sub
Sub GetWord(strFilePath As String)
'Bind to an existing or created instance of Microsoft Word
Dim objApp As Object
'Attempt to bind to an open instance
On Error Resume Next
Set objApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'Could not get instance, so create a new one
Err.Clear
On Error GoTo ErrHandler
Set objApp = CreateObject("Word.Application")
With objApp
.Visible = True
End With
Else
'Bound to instance, activate error handling
On Error GoTo ErrHandler
End If
'Open the file
objApp.Documents.Open strFilePath
ErrHandler:
'Release the object and resume normal error handling
Set objApp = Nothing
On Error GoTo 0
End Sub
Sub OpenFile()
Dim fn As Variant
ChDir "C:\Program Files\FolderName"
fn = Application.GetOpenFilename("All files,*.*,Excel-files,*.xls,Word Files,*.doc,PDF Files,*.pdf,", _
1, "Folder Name- Select folder and file to open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
' the user didn't select a file
Debug.Print "Selected file: " & fn
Select Case Right(fn, 3)
Case Is = "xls"
Workbooks.Open fn
Case Is = "doc"
Call GetWord(CStr(fn))
Case Else
MsgBox "How did you select a non xls or doc file?"
End Select
End Sub
Sub GetWord(strFilePath As String)
'Bind to an existing or created instance of Microsoft Word
Dim objApp As Object
'Attempt to bind to an open instance
On Error Resume Next
Set objApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'Could not get instance, so create a new one
Err.Clear
On Error GoTo ErrHandler
Set objApp = CreateObject("Word.Application")
With objApp
.Visible = True
End With
Else
'Bound to instance, activate error handling
On Error GoTo ErrHandler
End If
'Open the file
objApp.Documents.Open strFilePath
ErrHandler:
'Release the object and resume normal error handling
Set objApp = Nothing
On Error GoTo 0
End Sub