+ Reply to Thread
Results 1 to 10 of 10

Using Excel VBA to close IE

  1. #1
    JakeyC
    Guest

    Using Excel VBA to close IE

    I have a macro that opens Internet Explorer from within Excel and
    follows a hyperlink.

    I now need to add some code to close this instance of IE and return to
    Excel just in the same way as you would when you click the 'X' in
    corner of the browser.

    Any clever people know some example code for this, or is it impossible
    from within Excel?

    Regards,
    JakeyC


  2. #2
    RB Smissaert
    Guest

    Re: Using Excel VBA to close IE

    It is a lot of code, but it will do the job:

    Option Explicit
    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, _
    lHolder)

    '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)

    Loop

    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, _
    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
    ERROROUT:

    On Error GoTo 0
    CloseApp = 0

    End Function


    Sub test()

    CloseApp "Microsoft Internet Explorer", "IEFrame"

    End Sub


    Just put it all in a normal module and run the Sub test.


    RBS


    "JakeyC" <[email protected]> wrote in message
    news:[email protected]...
    >I have a macro that opens Internet Explorer from within Excel and
    > follows a hyperlink.
    >
    > I now need to add some code to close this instance of IE and return to
    > Excel just in the same way as you would when you click the 'X' in
    > corner of the browser.
    >
    > Any clever people know some example code for this, or is it impossible
    > from within Excel?
    >
    > Regards,
    > JakeyC
    >



  3. #3
    JakeyC
    Guest

    Re: Using Excel VBA to close IE

    Wow a lot of code indeed.

    I'll give it a try!

    Thanks!


  4. #4
    JakeyC
    Guest

    Re: Using Excel VBA to close IE

    Also just dicvovered the Browser Control - I guess I could also use
    this and then close (i.e. unload) the form in which it appears?


  5. #5
    RB Smissaert
    Guest

    Re: Using Excel VBA to close IE

    Maybe you could use that, but although it is a lot of code the API method
    looks simpler to me.

    RBS

    "JakeyC" <[email protected]> wrote in message
    news:[email protected]...
    > Also just dicvovered the Browser Control - I guess I could also use
    > this and then close (i.e. unload) the form in which it appears?
    >



  6. #6
    Jake Marx
    Guest

    Re: Using Excel VBA to close IE

    JakeyC,

    JakeyC wrote:
    > Also just dicvovered the Browser Control - I guess I could also use
    > this and then close (i.e. unload) the form in which it appears?


    You could automate IE as well:

    Sub Demo()
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")

    ie.navigate "http://www.longhead.com/"
    Do While ie.busy And Not ie.readystate = 4
    DoEvents
    Loop

    ie.Visible = True
    Application.Wait Now + TimeSerial(0, 0, 5)
    ie.Quit

    Set ie = Nothing
    End Sub

    --
    Regards,

    Jake Marx
    www.longhead.com


    [please keep replies in the newsgroup - email address unmonitored]



  7. #7
    RB Smissaert
    Guest

    Re: Using Excel VBA to close IE

    Thanks for the tip.

    RBS

    "Jake Marx" <[email protected]> wrote in message
    news:O90ro%[email protected]...
    > JakeyC,
    >
    > JakeyC wrote:
    >> Also just dicvovered the Browser Control - I guess I could also use
    >> this and then close (i.e. unload) the form in which it appears?

    >
    > You could automate IE as well:
    >
    > Sub Demo()
    > Dim ie As Object
    >
    > Set ie = CreateObject("InternetExplorer.Application")
    >
    > ie.navigate "http://www.longhead.com/"
    > Do While ie.busy And Not ie.readystate = 4
    > DoEvents
    > Loop
    >
    > ie.Visible = True
    > Application.Wait Now + TimeSerial(0, 0, 5)
    > ie.Quit
    >
    > Set ie = Nothing
    > End Sub
    >
    > --
    > Regards,
    >
    > Jake Marx
    > www.longhead.com
    >
    >
    > [please keep replies in the newsgroup - email address unmonitored]
    >
    >



  8. #8
    RB Smissaert
    Guest

    Re: Using Excel VBA to close IE

    The advantage though of starting the browser with
    ActiveWorkbook.FollowHyperlink
    is that it will always load the default browser and this may not be IE.
    Admittedly, then when you close the browser with the API you will need some
    extra code
    to cater for other browsers.

    RBS

    "Jake Marx" <[email protected]> wrote in message
    news:O90ro%[email protected]...
    > JakeyC,
    >
    > JakeyC wrote:
    >> Also just dicvovered the Browser Control - I guess I could also use
    >> this and then close (i.e. unload) the form in which it appears?

    >
    > You could automate IE as well:
    >
    > Sub Demo()
    > Dim ie As Object
    >
    > Set ie = CreateObject("InternetExplorer.Application")
    >
    > ie.navigate "http://www.longhead.com/"
    > Do While ie.busy And Not ie.readystate = 4
    > DoEvents
    > Loop
    >
    > ie.Visible = True
    > Application.Wait Now + TimeSerial(0, 0, 5)
    > ie.Quit
    >
    > Set ie = Nothing
    > End Sub
    >
    > --
    > Regards,
    >
    > Jake Marx
    > www.longhead.com
    >
    >
    > [please keep replies in the newsgroup - email address unmonitored]
    >
    >



  9. #9
    Jake Marx
    Guest

    Re: Using Excel VBA to close IE

    RB Smissaert wrote:
    > The advantage though of starting the browser with
    > ActiveWorkbook.FollowHyperlink
    > is that it will always load the default browser and this may not be
    > IE. Admittedly, then when you close the browser with the API you will
    > need some extra code
    > to cater for other browsers.


    That's true - it's definitely a tradeoff. There may be some users that
    don't want anything but their default browser launched. But for in-house
    apps, assuming IE use is often OK.

    --
    Regards,

    Jake Marx
    www.longhead.com


    [please keep replies in the newsgroup - email address unmonitored]



  10. #10
    Walter Contrata
    Guest

    Re: Using Excel VBA to close IE

    JakeyC,

    Please try the Quit method.

    Set IE = New InternetExplorer
    IE.Visible = True
    IE.GoHome
    ....
    IE.Quit

    Funny you should write. I'm trying to learn how to control IE too. The
    above is about all I can do so far!

    Best Regards,
    Walter

    *** Sent via Developersdex http://www.developersdex.com ***

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1