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.
??
rbendAsked:
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.

Erick37Commented:
Try this:

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private 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
Private Const WM_CLOSE = &H10


Private Sub Command1_Click()
    Dim lhWnd As Long
    Dim sApp As String
    'Exact Title of application
    sApp = "Calculator"
    'Find the window handle
    lhWnd = FindWindow(vbNullString, sApp)
    If lhWnd <> 0 Then
        'Post a close message to the application's window
        Call PostMessage(lhWnd, WM_CLOSE, 0&, 0&)
    End If
End Sub
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
rbendAuthor Commented:
looks like you're real close to what I need with this Erick37.
Just one adjustment please.
Close the window if I know just part of the titel. Ex. Close Microsoft Internet Explorer if the title is:
Your Visual Basic Question - Microsoft Internet Explorer
??

0
rbendAuthor Commented:
looks like you're real close to what I need with this Erick37.
Just one adjustment please.
Close the window if I know just part of the titel. Ex. Close Microsoft Internet Explorer if the title is:
Your Visual Basic Question - Microsoft Internet Explorer
??

0
Erick37Commented:
Part of title:
This code uses EnumWindows to retrieve the title of all windows in the system, compares it with the partial name, and stops when a match is made.  Code modified from a sample available at www.thescarms.com


'~~~~~~~FORM CODE~~~~~~~~~~~~~~
Option Explicit
Private Sub Command1_Click()
    Dim sApp As String
    'Find notepad with partial name
    sApp = "notepa"
    glHwnd = SearchWindows(sApp, Me.hwnd)
    'End application if found
    If glHwnd > 0 Then
        PostMessage glHwnd, WM_CLOSE, 0&, 0&
    End If
End Sub

'~~~~~~~MODULE CODE~~~~~~~~~~~~
Option Explicit

Public Const WM_CLOSE = &H10
Public Const MAX_PATH = 260

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) 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
Declare Function EnumWindows Lib "user32" _
    (ByVal lpEnumFunc As Long, ByVal lParam 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

Private sAppTitle As String
Public glHwnd As Long

Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
    Dim lResult    As Long
    Dim sWndName   As String
   
    fEnumWindowsCallBack = 1
    sWndName = Space$(MAX_PATH)
   
    lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
    sWndName = Left$(sWndName, lResult)
    'Search Title for our string
    If (InStr(1, sWndName, sAppTitle, vbTextCompare) > 0) Then
        Debug.Print sWndName
        glHwnd = hwnd
        fEnumWindowsCallBack = 0
    End If
End Function

Public Function SearchWindows(sApp As String, hwnd As Long) As Long
    sAppTitle = sApp
    glHwnd = 0
    Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
    SearchWindows = glHwnd
End Function
0
rbendAuthor Commented:
Exactly it..thanks.
0
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.