Option Explicit
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Const WM_CLOSE = &H10
'THIS WILL CLOSE AN APP BY CAPTION ONLY
Public Sub CloseProgram(ByVal Caption As String)
On Error GoTo EVT
Dim lngWin As Long
lngWin = FindWindow(vbNullString, Caption)
If lngWin = 0 Then Exit Sub
SendMessage lngWin, WM_CLOSE, 0&, 0&
Exit Sub
EVT:
Err.Clear
Resume Next
End Sub
call the code above by Call CloseProgram("My Applications Caption")
Please note that the code above will terminate without notcie if an error occures.
This is to make it as stealth as possible. Avoiding bugs and stuff.
If anyone has a good example on how to do this without knowing any part of the caption, basically doing it by closing a runing EXE file, I would very much appreciate an email about that.
Comments