Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Killing a window/userform based on its caption.

VBA How To

Killing a window/userform based on its caption.

by  taupirho  Posted    (Edited  )


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

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top