sendmessage to close window does not work

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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&
Try adding this into your module:

Public hand As Long

Then change the sendmessage call to:

SendMessage hand, WM_CLOSE, 0&, 0&

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rjefAuthor Commented:

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.