sendmessage to close window does not work

Posted on 2004-11-23
Last Modified: 2013-12-25
Why doesn't the below code work.  
"C:\closewin\WdsToClose1.txt" contains screen titles to close.
"c:\closewin\allwin.txt" will contain all windows that have taken focus.
'set timer1 interval to 100
' form code
Private Sub Form_Load()
xx = 0
Open "C:\closewin\WdsToClose1.txt" For Input As #1
Do While Not EOF(1)
xx = xx + 1
Line Input #1, wdstoclose(xx)
Close #1
End Sub
Function foreGWin() As String
Dim textlen As Long
Dim wintext As String
Dim slength As Long
Dim fgHwnd As Long
fgHwnd = GetForegroundWindow()
textlen = GetWindowTextLength(fgHwnd) + 1
wintext = Space(textlen)
slength = GetWindowText(fgHwnd, wintext, textlen)
wintext = Left(wintext, slength)
foreGWin = wintext
hand = fgHwnd
End Function
Public Sub WaitForWindow(WinHead As String)
    If Left(foreGWin(), Len(WinHead)) = WinHead Then
    SendMessage hWnd, WM_CLOSE, 0&, ByVal 0&
    End If
  If curwin <> foreGWin() Then
    Open "c:\closewin\allwin.txt" For Append As #2
        If foreGWin() <> "" Then
            Print #2, Format(Date, "MM/DD/YY") & " "; Format(Time, "HH:MM") & " " & foreGWin()
        End If
    curwin = foreGWin()
    Close #2
  End If
  End Sub
Private Sub Timer1_Timer()
For hh = 1 To xx
    Call WaitForWindow(wdstoclose(hh))
Next hh
End Sub

' module
Public Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal Y As Long) As Long
Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Public Const WM_CLOSE = &H10
Public 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 curwin As String
Public xx As Double
Public wdstoclose(10000) As String
Public hh As Double

Question by:rjef
    LVL 28

    Expert Comment

    The hWnd in the sendmessage call is for your forms window,not the form you are trying to close.

    SendMessage hWnd, WM_CLOSE, 0&, 0&
    LVL 28

    Accepted Solution

    Try adding this into your module:

    Public hand As Long

    Then change the sendmessage call to:

    SendMessage hand, WM_CLOSE, 0&, 0&

    Author Comment


    Can you make it cycle through all running processes and then close any that are in the WdsToClose1.txt file.  I need this because sometimes the window i want closed is not the active window

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now