Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBA Visual Basic for Applications (Microsoft) FAQ

VBA How To

Close the Internet Explorer Window by jennuhw
Posted: 12 Oct 05

I have been searching on how to close the current IE window that Excel has opened up in.  I found this code on Microsoft NewsGroups.  It's a lot of code, but you only have to change one line.
Put this following code in a new module:


Private Declare Function PostMessage Lib "user32" _
                                     Alias "PostMessageA" _
                                     (ByVal hwnd As Long, _
                                      ByVal wMsg As Long, _
                                      ByVal wParam As Long, _
                                      lParam As Any) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" _
                                   (ByVal hwnd As Long, _
                                    ByVal wCmd 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
Private Declare Function GetClassName Lib "user32" _
                                      Alias "GetClassNameA" _
                                      (ByVal hwnd As Long, _
                                       ByVal lpClassName As String, _
                                       ByVal nMaxCount As Long) _
                                       As Long
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDLAST = 1
Private Const GW_HWNDNEXT = 2
Private Const GW_HWNDPREV = 3
Private Const GW_OWNER = 4
Private Const GW_CHILD = 5
Private Const WM_CLOSE = &H10

Function FindWindowHwndLike(hWndStart As Long, _
                            ClassName As String, _
                            WindowTitle As String, _
                            level As Long, _
                            lHolder As Long) As Long

    'finds the first window where the class name start with ClassName
    'and where the Window title starts with WindowTitle, returns Hwnd
    Dim hwnd As Long
    Dim sWindowTitle As String
    Dim sClassName As String
    Dim r As Long

    'Initialize if necessary. This is only executed
    'when level = 0 and hWndStart = 0, normally
    'only on the first call to the routine.
    If level = 0 Then
        If hWndStart = 0 Then
            hWndStart = GetDesktopWindow()
        End If
    End If

    'Increase recursion counter
    level = level + 1

    'Get first child window
    hwnd = GetWindow(hWndStart, GW_CHILD)

    Do Until hwnd = 0

        'Search children by recursion
        lHolder = FindWindowHwndLike(hwnd, _
                                     ClassName, _
                                     WindowTitle, _
                                     level, _

        'Get the window text
        sWindowTitle = Space$(255)
        r = GetWindowText(hwnd, sWindowTitle, 255)
        sWindowTitle = Left$(sWindowTitle, r)

        'get the class name
        sClassName = Space$(255)
        r = GetClassName(hwnd, sClassName, 255)
        sClassName = Left$(sClassName, r)

        If InStr(1, sWindowTitle, WindowTitle, vbBinaryCompare) > 0 And _
           sClassName Like ClassName & "*" Then
            FindWindowHwndLike = hwnd
            lHolder = hwnd
            Exit Function
        End If

        'Get next child window
        hwnd = GetWindow(hwnd, GW_HWNDNEXT)


    FindWindowHwndLike = lHolder

End Function

Function CloseApp(ByVal strApp As String, _
                  ByVal strClass As String) As Long

    'will find a window based on:
    'the partial start of the Window title and/or
    'the partial start of the Window class
    'and then close that window
    'for example, this will close Excel:
    'CloseApp "", "XLM" and this will:
    'CloseApp "Microsoft Excel", ""
    'but this won't: CloseApp "", "LM"
    'it will only close the first window that
    'fulfills the criteria
    'will return Hwnd if successfull, and 0 if not

    Dim hwnd As Long

    On Error GoTo ERROROUT

    hwnd = FindWindowHwndLike(0, _
                              strClass, _
                              strApp, _
                              0, _

    If hwnd = 0 Then
        CloseApp = 0
        Exit Function
    End If

    'Post a message to the window to close itself
    PostMessage hwnd, WM_CLOSE, 0&, 0&

    CloseApp = hwnd

    Exit Function

    On Error GoTo 0
    CloseApp = 0

End Function

Code to call the close:


CloseApp "Microsoft Internet Explorer", "IEFrame"

I left it as CloseApp "Microsoft Internet Explorer", "" and it only closed the current IE window.  If it is ran directly in the Excel app, nothing closes!  

Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close