PostMessage glHwnd, WM_CLOSE, 0&, 0&

Posted on 2005-04-30
Medium Priority
Last Modified: 2006-11-18
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???
Question by:John Account
LVL 13

Expert Comment

ID: 13901313

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).
LVL 28

Expert Comment

ID: 13901328
Have you tried with SendMessage?

Author Comment

by:John Account
ID: 13901362
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?
LVL 13

Expert Comment

ID: 13901442
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
LVL 29

Accepted Solution

nffvrxqgrcfqvvc earned 2000 total points
ID: 13908445
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
        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)
    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")

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question