Close an application
By: Lorenzo Dalla Vecchia Date: 19/03/2000

With the use of API, you can specify the title of a program, search for it, and close it down.

Start with adding the following API Declarations to the Declarations section of a module:

Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (
ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10

And here's the code. Just replace apptitle with the exact title of the application you want to close (i.e. "Calculator").

Dim winHwnd As Long
Dim RetVal As Long

winHwnd = FindWindow(vbNullString, "

If winHwnd <> 0 Then
    RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
        MsgBox "Error posting message."
End If
    MsgBox "Application is not open."
End If


Click Here!

Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: