​A selection of topics on IT and its application to finance. 
​Send me your comments, questions ​or suggestions by clicking
h​ere


elmama
Killing a window based on its caption text


Here's some example code to show you how to find and kill a window based on what its caption is displaying


Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc 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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Public Function EnumWindowsProc1(ByVal hwnd As Long, ByVal lparam As Long) As Boolean
Dim ssave As String, ret As Long
ret = GetWindowTextLength(hwnd)
ssave = Space(ret)
GetWindowText hwnd, ssave, ret + 1
If ssave = "Your window caption goes here" Then
x = PostMessage(hwnd, &H12, 0, 0)
EnumWindowsProc1 = False
Else
EnumWindowsProc1 = True
End If

End Function

'
' Call the above function like this
'
Sub killwin1()

EnumWindows AddressOf EnumWindowsProc1, ByVal 0&

End Sub