About This Code
Brief Description:
Using ShellExecuteEx to open a file with the associated Windows application
Contributor:
Charles Robinson
Notes Version:
R4.x, R6.x, R5.x, R7.x
Last Modified:
24 Aug 2006
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
If you want to launch a file using the associated application in Windows, use this code. It also uses TerminateProcess to close the application after you open it.
(Declarations)
Const SEE_MASK_NOCLOSEPROCESS = &H40
Const SEE_MASK_FLAG_NO_UI = &H400
Private 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
Declare Function ShellExecuteEx Lib "shell32.dll"_
Alias "ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long
Declare Function TerminateProcess Lib "kernel32"_
Alias "TerminateProcess" (Byval hProcess As Long, Byval uExitCode As Long) As Long
Sub Initialize
Dim SEI As SHELLEXECUTEINFO
SEI.cbSize = Len(SEI)
SEI.fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_FLAG_NO_UI
SEI.lpVerb = "open"
SEI.lpFile = "c:\sample.txt"
SEI.nShow = 1
SEI.hInstApp = 0
SEI.lpIDList = 0
Call ShellExecuteEx(SEI)
Messagebox "Application opened.", 0, "Success"
Call TerminateProcess(SEI.hProcess, 0)
End Sub
Usage / Example