Close a window if I know the Title or Part of the Title of the window..

please don't offer the solution that includes:
If InStr(temp, "Internet
Explorer") <> 0 Then SendKeys "%{F4}",
On my 500 mhz machine, it processes so fast that ALL my windows close with sendkeys.
Another offer please?
This app must detect the opening of a program's window (ie. Netscape) then close it in 2 seconds.
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.

 first - the SendKeys will close only that window if it's second parameter will be TRUE.
since you already have the window HWND -you can use the API DestroyWindow(hwnd)
to close it .
Private Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long

rbendAuthor Commented:
I don't want to have to have the HWND of the window, I just have the title.
This quesition was asked twice by mistake.
pLease answer the other one.
what an ignorance !!!

no way to read the window title or part of it without the window HWND.
in the other similar Q - you got a solution that mentions HWND or Window HANDLE (same thing) at least 5 times...

if you don't want to have the HWND of the window (as you say) - you don't want to close it as well....and what's worse - you don't want to learn a thing
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

if you increase the points i'll send for you a code to control any opened window using API ,, it's very useful ..
This example shows how to find a handle of an application just knowing the title or part of the title...  It's a 16-bit example, but I've included the 32bit function here.

VB3 How to Get Window Handle Without Specifying Exact Title 

Once you have the handle, you can use DestroyWindow to close it...



Private Const GWL_ID = (-12)
Global ProgHandle As Long

Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5

Declare Function SetFocusAPI Lib "user32" Alias "SetForegroundWindow" _
    (ByVal hwnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
    ByVal wCmd As Long) As Long
Private Declare Function GetWindowLW Lib "user32" Alias "GetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
    (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Declare Function GetDesktopWindow Lib "user32" () As Long

Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long

Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

Declare Function SysSetFocus Lib "user32" Alias "SetFocus" _
    (ByVal hwnd As Long) As Long

Function FindWindowLike(hWndArray() As Variant, ByVal hWndStart As Long, WindowText As String, Classname As String, ID) As Long
    Dim hwnd As Long
    Dim r As Long
    ' Hold the level of recursion:
    Static level As Long
    ' Hold the number of matching windows:
    Static iFound As Long
    Dim sWindowText As String
    Dim sClassname As String
    Dim sID
    ' Initialize if necessary:
    If level = 0 Then
        iFound = 0
        ReDim hWndArray(0 To 0)
        If hWndStart = 0 Then hWndStart = GetDesktopWindow()
    End If
    ' Increase recursion counter:
    level = level + 1
    ' Get first child window:
    hwnd = GetWindow(hWndStart, GW_CHILD)
    Do Until hwnd = 0
        DoEvents ' Not necessary
        ' Search children by recursion:
        r = FindWindowLike(hWndArray(), hwnd, WindowText, Classname, ID)
        ' Get the window text and class name:
        sWindowText = Space(255)
        r = GetWindowText(hwnd, sWindowText, 255)
        sWindowText = Left(sWindowText, r)
        sClassname = Space(255)
        r = GetClassName(hwnd, sClassname, 255)
        sClassname = Left(sClassname, r)
        ' If window is a child get the ID:
        If GetParent(hwnd) <> 0 Then
            r = GetWindowLW(hwnd, GWL_ID)
            sID = CLng("&H" & Hex(r))
            sID = Null
        End If
        ' Check that window matches the search parameters:
        If sWindowText Like WindowText And sClassname Like Classname Then
            If IsNull(ID) Then
                ' If find a match, increment counter and
                '  add handle to array:
                iFound = iFound + 1
                ReDim Preserve hWndArray(0 To iFound)
                hWndArray(iFound) = hwnd
            ElseIf Not IsNull(sID) Then
                If CLng(sID) = CLng(ID) Then
                    ' If find a match increment counter and
                    '  add handle to array:
                    iFound = iFound + 1
                    ReDim Preserve hWndArray(0 To iFound)
                    hWndArray(iFound) = hwnd
                End If
            End If
        End If
        ' Get next child window:
        hwnd = GetWindow(hwnd, GW_HWNDNEXT)
    ' Decrement recursion counter:
    level = level - 1
    ' Return the number of windows found:
    FindWindowLike = iFound
End Function

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
rbendAuthor Commented:
Thank you all..

Thanks for the points! Glad I could help you.

AnswerTheMan said:

     "what an ignorance !!! no way to read the window title or part of it without the window HWND..."

I think you owe rbend an apology...

mcrider : after searching your fine answer for the pharse 'HWND' - i stopped counting after the 5th 'HWND'.

the man wanted to do that without using the 'HWND' term, and i said no one can offer a code that do that without the 'HWND' .

what have i to appologize ?

rbend's english is not that good... but he/she clearly stated that "I just have the title.", meaning that rbend did not know what the hwnd was for the window that is needed to be destroyed.

Instead of carefully reading the question, you just "went off".  That is what you should apologize for...


Again, glad I could help!


mcrider : Wrong again.....

this Q is a follow up to a privious RBEND's Q in which i have theached him how to use "GetForegroudWindow" and "GetWindowText" to retrive that caption that he have now... in this Q...

there - he asked : if that caption includes some string - how to terminate it ... and i suggested to send ALT+F4 (it's the foreground window).

that's the background to this Q, which you are not aware of.

so,he has the HWND if he wants it or not....

now, you accusing me : <<"Instead of carefully reading the question, you just "went off".  That is what you should apologize for...">>

read this very comment carfully and you come to conclusion that maybe you owe me some apologize....

never mind. BTW - i'm not an ENGLISH speaker myself.....


rbend never mentions a previous question in this question and has accepted my response. Please read the question again.

sure he does not mention....
you can check Q.10236498 .
it's good for you. it's free, still open, and after my non apologizing you may collect the points i still have not got there inspite of RBEND saying there to me "Your answer is fine".

i'm glad for you RBEND accepted you responce. anyway...i'm quite sure he won't get far in this tread. his human HWND just not meet the requirments.
rbendAuthor Commented:
You all have been helpful.
I thank you.
My HWND thanks you.
Merry Christmas
be well
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.