'Open any file (application based on file extension)
Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
'
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400
Public Const SW_SHOWNORMAL = 1
Public Const SE_ERR_FNF = 2
Public Const SE_ERR_NOASSOC = 31
'
Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As SHELLEXECUTEINFO) As Long
'
Public Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal _
dwMilliseconds As Long) As Long
Public Const INFINITE = &HFFFF
Public Const WAIT_TIMEOUT = &H102
'
Public Function strOpenFile(ByVal pstrFilename As String, Optional ByVal pstrParameters As String = "", Optional ByVal pysnWait As Boolean = False) As String
Dim SEI As SHELLEXECUTEINFO
Dim lngRetVal As Long
Dim strResult As String
strResult = ""
With SEI
.cbSize = Len(SEI)
.fMask = SEE_MASK_NOCLOSEPROCESS ' Or SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
.hwnd = Application.hWndAccessApp
.lpVerb = "open"
.lpFile = pstrFilename
.lpParameters = pstrParameters
.lpDirectory = vbNullChar
.nShow = SW_SHOWNORMAL
.hInstApp = 0
.lpIDList = 0
End With
lngRetVal = ShellExecuteEx(SEI)
If lngRetVal = 0 Then
' The function failed, so report the error. Err.LastDllError
' could also be used instead, if you wish.
Select Case SEI.hInstApp
Case SE_ERR_FNF
strResult = "The file '" & pstrFilename & "' was not found."
Case SE_ERR_NOASSOC
strResult = "No program is associated with '" & pstrFilename & "'"
Case Else
strResult = "An unexpected error occured.(" & lngRetVal & ")"
End Select
ElseIf pysnWait Then
' Wait for the opened process to close before continuing. Instead
' of waiting once for a time of INFINITE, this example repeatedly checks to see if the
' is still open. This allows the DoEvents VB function to be called, preventing
' our program from appearing to lock up while it waits.
Do
DoEvents
lngRetVal = WaitForSingleObject(SEI.hProcess, 0)
Loop While lngRetVal = WAIT_TIMEOUT
End If
strOpenFile = strResult
End Function