I did a little research on how to close an IE window and found a site that offers a decent solution, except it does not work 100%. I am not sure if it is ok to include a link to another forum so I'll leave it out unless someone can confirm it's ok to include. The first block of code closes all IE windows except for those containing a user defined string (the code refers to this as 'blocked' (the string 'Etreme' is blocked).
Underneath this code is a modifed version (of the code) that I tweaked to do the opposite, close only windows containing a user defined string. To test this code, open 2 IE windows, use one to search for 'testing' and run the code. the testing IE will close and the other will remain open. It works but the looping never stops. I guess the do-while is not setup correctly. Does anyone know how to fix this?
BTY, as I was checking this out it appears the classic way to approach this is to have the process responsible for launching the IE window pass a PID back to VBA and then to use a simple API to kill that PID. I am using shellexicute to send a URL to a datamart and that process opens IE. There is no way to pass a variable back using shellexicute so the PID approach won't work, unless someone knows better. This code gets around the lack of hand shaking nicely.
This is the starting point, please focus on code below this
Code:
Private Sub CloseWindow()
Dim lngWindow As Long, lngWindowLast As Long
Dim strClassSought As String: strClassSought = "IEFrame" 'type of window to close
Dim strTitleBlocked As String: strTitleBlocked = "Xtreme" 'windows to exclude
Dim strClass As String, strTitle As String
Dim i As Integer
lngWindow = GetWindow(Application.hwnd, GW_HWNDFIRST)
lngWindowLast = GetWindow(Application.hwnd, GW_HWNDLAST)
'now loop through all windows
Do While lngWindow <> lngWindowLast
'create a buffer for the class name and the window title
strClass = Space(100)
strTitle = Space(100)
'get the class name and the window title
GetWindowText lngWindow, strTitle, 100
GetClassName lngWindow, strClass, 100
'check whether this window matches the criteria
If InStr(strClass, strClassSought) <> 0 And InStr(strTitle, strTitleBlocked) = 0 Then
'post a message to the window to close itself
PostMessage lngWindow, WM_CLOSE, 0&, 0&
'start all over
lngWindow = GetWindow(Application.hwnd, GW_HWNDFIRST)
Else
'get the next window
lngWindow = GetWindow(lngWindow, GW_HWNDNEXT)
End If
C1 = C1 + 1: Application.StatusBar = C1: DoEvents
Loop
End Sub
This code closes only windows containing a user defined string
Code:
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 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
Const WM_CLOSE = &H10
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Private Sub CloseWindow()
Dim lngWindow As Long, lngWindowLast As Long
Dim strClassSought As String: strClassSought = "IEFrame" 'type of window to close
Dim strTitleToClose As String: strTitleToClose = "testing" 'windows to include
Dim strClass As String, strTitle As String
Dim i As Integer
lngWindow = GetWindow(Application.hwnd, GW_HWNDFIRST)
lngWindowLast = GetWindow(Application.hwnd, GW_HWNDLAST)
'now loop through all windows
Do While lngWindow <> lngWindowLast
'create a buffer for the class name and the window title
strClass = Space(100)
strTitle = Space(100)
'get the class name and the window title
GetWindowText lngWindow, strTitle, 100
GetClassName lngWindow, strClass, 100
'check whether this window matches the criteria
If InStr(strClass, strClassSought) <> 0 And InStr(strTitle, strTitleToClose) <> 0 Then
'post a message to the window to close itself
PostMessage lngWindow, WM_CLOSE, 0&, 0&
'start all over
lngWindow = GetWindow(Application.hwnd, GW_HWNDFIRST)
Else
'get the next window
lngWindow = GetWindow(lngWindow, GW_HWNDNEXT)
End If
C1 = C1 + 1: Application.StatusBar = C1: DoEvents
Loop
End Sub