' Will open a file using the application indicated by the file extension.
References must be made to all of these application libraries installed on your PC.
Also should set debugging to "BREAK ON UNHANDLED ERRORS"
Public Function OpenMyOfficeApp(ByVal FileOfInterest As String)
On Error GoTo BYE
LastPeriodPos = InStrRev(FileOfInterest, ".")
If LastPeriodPos > 0 Then
MyFileExtension = Right(FileOfInterest, Len(FileOfInterest) - LastPeriodPos)
Else
MyFileExtension = "none"
End If
Dim OpenWithApp As String
Select Case LCase(MyFileExtension)
Case "xls"
OpenWithApp = "Excel.Application"
Case "wks"
OpenWithApp = "Excel.Application"
Case "xla"
OpenWithApp = "Excel.Application"
Case "mdb"
OpenWithApp = "Access.Application"
Case "mde"
OpenWithApp = "Access.Application"
Case "ppt"
OpenWithApp = "Powerpoint.Application"
Case "rtf"
OpenWithApp = "Word.Application"
Case "doc"
OpenWithApp = "Word.Application"
Case "txt"
OpenWithApp = "Word.Application"
Case "none"
OpenWithApp = "Word.Application"
Case Else
MsgBox "Cannot recognize file type, so will attempt to open it!", vbOKOnly, "File Type Unrecognized"
Exit Function
End Select
Dim objOfMyAffection
On Error GoTo APPLICATION_CREATE
Set objOfMyAffection = GetObject(, OpenWithApp)
GoTo SKIP_APPLICATION_CREATE
APPLICATION_CREATE:
Set objOfMyAffection = CreateObject(OpenWithApp)
SKIP_APPLICATION_CREATE:
On Error GoTo BYE
' ***************** Open Methods will Vary *********************************
Select Case LCase(MyFileExtension)
Case "xls"
objOfMyAffection.Workbooks.Open (FileOfInterest)
objOfMyAffection.Visible = True
Case "wks"
objOfMyAffection.Workbooks.Open (FileOfInterest)
objOfMyAffection.Visible = True
Case "xla"
objOfMyAffection.Workbooks.Open (FileOfInterest)
objOfMyAffection.Visible = True
Case "mdb"
objOfMyAffection.OpenCurrentDatabase FileOfInterest
objOfMyAffection.Visible = True
Case "mde"
objOfMyAffection.OpenCurrentDatabase FileOfInterest
objOfMyAffection.Visible = True
Case "ppt"
objOfMyAffection.Activate
objOfMyAffection.Visible = True
objOfMyAffection.Presentations.Open (FileOfInterest), ReadOnly:=msoFalse
Case "rtf"
objOfMyAffection.Documents.Open (FileOfInterest)
objOfMyAffection.Visible = True
objOfMyAffection.Activate
Case "doc"
objOfMyAffection.Documents.Open (FileOfInterest)
objOfMyAffection.Visible = True
objOfMyAffection.Activate
Case "txt"
objOfMyAffection.Documents.Open (FileOfInterest)
objOfMyAffection.Visible = True
objOfMyAffection.Activate
End Select
BYE:
End Function