PostMessage glHwnd, WM_CLOSE, 0&, 0&

I'm using this function to close a window containing certain titles I don't want to appear: PostMessage glHwnd, WM_CLOSE, 0&, 0&

However, if I get a popup window that reads "Internet Explorer Script Error"--it will not close this window. Unfortunately, the user must click either Yes or No in that error popup window box.

So...my question is: How can I get my application to automatically close that window?  Maybe I can automate a way in which my app will select No in that error prompt, to close it???
John AccountAsked:
Who is Participating?
 
nffvrxqgrcfqvvcCommented:
Try this...It will close the window based on the title..it doesnt have to be exact it works even if you specify a partial title..ie.. if i type internet    then it will close internet explorer window...etx...

'Add the following code a .BAS module
'===========================

Option Explicit

Declare Sub ReleaseCapture Lib "user32" ()
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
Declare Function getwindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd 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
Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long)
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
    Public Const WM_CLOSE = &H10
    Public AppTitle As String
    Public ApphWnd As Long
Function GetCaption(WindowhWnd)
   Dim hwndlength As Long
   Dim hWndTitle As String
   Dim a As Long
    hwndlength = GetWindowTextLength(WindowhWnd)
    hWndTitle = String$(hwndlength, 0)
    a = GetWindowText(WindowhWnd, hWndTitle, (hwndlength + 1))
    GetCaption = hWndTitle
End Function
Function CheckAllWindows(ByVal hwnd As Long, lParam As Long) As Boolean
    Dim a
    a = LCase(GetCaption(hwnd))


    If InStr(1, a, LCase(AppTitle)) <> 0 Then
        ApphWnd = hwnd
        CheckAllWindows = False
    Else
        CheckAllWindows = True
    End If
End Function


Sub KillWin(Title As String)
    Dim a
    AppTitle = Title


EnumWindows AddressOf CheckAllWindows, 0&
    If ApphWnd = 0 Then Exit Sub
    a = PostMessage(ApphWnd, WM_CLOSE, 0&, 0&)
End Sub
Public Sub FormDrag(TheForm As Form)
    ReleaseCapture
    Call SendMessage(TheForm.hwnd, &HA1, 2, 0&)
End Sub


'====================
'Form code

'add a command button and just add the following inside the command1_click

Call KillWin("Internet Explorer Script")
0
 
iHadiCommented:
Hi

Try the DestroyWindow API

Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long

The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages to the window to deactivate it and remove the keyboard focus from it. The function also destroys the window’s menu, flushes the thread message queue, destroys timers, removes clipboard ownership, and breaks the clipboard viewer chain (if the window is at the top of the viewer chain).
0
 
vinnyd79Commented:
Have you tried with SendMessage?
0
 
John AccountAuthor Commented:
Hmmmmmnn. Can you give me an example of how to use it, iHadi--how to call it, etc? Thanks

No, vinnyd79. I don't know how I would do that. You think that would work? If so, how, please?
0
 
iHadiCommented:
Nothing Much

Declare the API as follows in a module

Public Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long

Write the following code in the sub that closes the window as follwos

tmpNum = DestroyWindow (glHwnd) <-- the handle of the window you want to close

'if  tmpNum <> 0 then the function succeeded, Else failed
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.