Give this a try:
Code:
Option Explicit
Option Compare Text
Declare Function EnumWindows Lib "user32" _
(ByVal wndenmprc As Long, _
ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
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 Const WM_CLOSE = &H10
Private mTarget As String
Public Sub TerminateTask(app_name As String)
mTarget = app_name
EnumWindows AddressOf EnumCallback, 0
End Sub
Public Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim Buffer As String * 256
Dim Title As String
Dim Length As Long
' Get the window's title.
Length = GetWindowText(app_hWnd, Buffer, Len(Buffer))
Title = Left$(Buffer, Length)
' See if this is the target window.
If InStr(Title, mTarget) <> 0 Then
' Kill the window.
SendMessage app_hWnd, WM_CLOSE, 0, 0
End If
EnumCallback = 1
End Function
You can call the TerminateTask sub with a parameter like "Internet Explorer" or "Microsoft Word". The specified app will be killed.
Hope this helps.
Shrog