[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


sendmessage to close window does not work

Posted on 2004-11-23
Medium Priority
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
  • 2
LVL 28

Expert Comment

ID: 12657124
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

vinnyd79 earned 2000 total points
ID: 12657195
Try adding this into your module:

Public hand As Long

Then change the sendmessage call to:

SendMessage hand, WM_CLOSE, 0&, 0&

Author Comment

ID: 12658398

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 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