Close Internet Explorer in VBA (1 Viewer)

stu_c

Registered User.
Local time
Today, 02:21
Joined
Sep 20, 2007
Messages
489
Hi all
I have an Access form that when filled in a button is pressed that opens up IE and fills out an online form, unfortunately if another IE page is open it comes back with an error as it cannot find the named fields, what VBA code would I need to close any open IE windows before it opens the new one?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:21
Joined
Jul 9, 2003
Messages
16,273
I noticed that your question appears to have been overlooked. I am bumping it up the list to give it another chance at receiving some attention…
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 20:21
Joined
Feb 28, 2001
Messages
27,147
This is a tricky little problem in security.

IE, like many tasks under Windows, is written via "pure code" techniques. This means that they use advanced memory management to instantiate the process. If you open a web page and without closing it, open another, you have ONE copy of the "pure" section of IE and two copies of the working memory ("impure" or "dirty") area. So when you switch from one page to the other (both still being open), it is merely a matter of diddling a few memory pointers to get to the right context.

What becomes trickier is, WHY is that other page open (if your Access-based task didn't open it)? Even if you are the owner of the machine and its only user, you open different session threads to run Access and IE simultaneously. If the already-open Window wasn't started by Access but rather was you doing a separate act of browsing, those two threads look different to Windows. It might, as a security measure, object to you trying to close a session from a thread that didn't open that same session. This is a razor-thin issue in that it is ALL you (if you are the only user on the machine) but Windows still has this internal mandate to keep threads separate based on complex criteria.

I am not deeply familiar with IE internals, but in theory the solution is to include IE in your app's reference list (see the link below) and then manipulate the pages to find the one you want and then make it current. Do not try to close the IE page you don't want. Instead, try to manipulate the page you DO want and just leave the other page alone.


You can find more by searching the web for "Use VBA to control IE" and see if anything there helps you.
 

stu_c

Registered User.
Local time
Today, 02:21
Joined
Sep 20, 2007
Messages
489
Tried so many different ones without any success
Dim IE As Object
Set IE = New InternetExplorer
IE.Visible = True
IE.GoHome
IE.Quit

etc

I Managed to get it to open and fill out the form but closing all IE pages seems another beast!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:21
Joined
May 7, 2009
Messages
19,229
with ever changing updates in our os, that won't be possible now.
you need API to close it.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:21
Joined
Sep 21, 2011
Messages
14,238
I would expect you could run a vbscript?
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:21
Joined
Sep 21, 2011
Messages
14,238
I can supply the script later, currently working on admin.
To call the script look here

Note it will close ALL instances of IE.

Edit: Here is the code. You can use it for any process, just change the name being searched for. I use to it to kill Chrome when it misbehaves.
Code:
' ProcessKillLocal.vbs
' Sample VBScript to kill a program
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.7 - December 2010
' ------------------------ -------------------------------'
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'iexplore.exe'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill

WScript.Quit
' End of WMI Example of a Kill Process
 
Last edited:

stu_c

Registered User.
Local time
Today, 02:21
Joined
Sep 20, 2007
Messages
489
Hello Gasman
Thanks for the below i have an error in red for: objProcess.Terminate() any suggestions?
I can supply the script later, currently working on admin.
To call the script look here

Note it will close ALL instances of IE.

Edit: Here is the code. You can use it for any process, just change the name being searched for. I use to it to kill Chrome when it misbehaves.
Code:
' ProcessKillLocal.vbs
' Sample VBScript to kill a program
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.7 - December 2010
' ------------------------ -------------------------------'
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'iexplore.exe'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill

WScript.Quit
' End of WMI Example of a Kill Process
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:21
Joined
Sep 21, 2011
Messages
14,238
And the error is? :(

This IS vbcript not vba?
Are you trying to run it as vba?
 

sonic8

AWF VIP
Local time
Today, 03:21
Joined
Oct 27, 2015
Messages
998
I have an Access form that when filled in a button is pressed that opens up IE and fills out an online form, unfortunately if another IE page is open it comes back with an error as it cannot find the named fields
I would like to second @The_Doc_Man 's remarks.
It shouldn't happen that an IE instance (=> browser tab) you created in code suddenly refers to a different browser tab or window the user created. I suggest you show the code you are using for this.

Besides, I think it is an extremely bad idea to programmatically close other windows or tabs the user opened for himself. This is a foolproof way to make users hate your application. - Leave the user's stuff alone!
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:21
Joined
Sep 21, 2011
Messages
14,238
I tried to convert it to VBA, but will need an expert to get the syntax correct for the Terminate.

Code:
Sub KillProcess()
Dim objWMIService As Object, objProcess As Object, colProcess As Object
Dim strComputer As String, strProcessKill As String
strComputer = "."
strProcessKill = "'iexplore.exe'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill)

For Each objProcess In colProcess
    colProcess.Terminate
Next
End Sub
 

Attachments

  • KillIE.PNG
    KillIE.PNG
    24.7 KB · Views: 137

KitaYama

Well-known member
Local time
Today, 10:21
Joined
Jan 6, 2022
Messages
1,540
I tried to convert it to VBA, but will need an expert to get the syntax correct for the Terminate.

Code:
Sub KillProcess()
Dim objWMIService As Object, objProcess As Object, colProcess As Object
Dim strComputer As String, strProcessKill As String
strComputer = "."
strProcessKill = "'iexplore.exe'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill)

For Each objProcess In colProcess
    colProcess.Terminate
Next
End Sub
I'm not an expert but this one closes outlook
Code:
Public Sub CloseApp()

    Dim oServ As Object
    Dim cProc As Object
    Dim oProc As Object
    StrProcessName = "Outlook.exe"
    Set oServ = GetObject("winmgmts:")
    Set cProc = oServ.ExecQuery("select * from win32_process")
    For Each oProc In cProc
        If InStr(1, oProc.Name, StrProcessName, vbTextCompare) <> 0 Then
            If InStr(1, oProc.CommandLine, NtpPath, vbTextCompare) <> 0 Then
            oProc.Terminate
            End If
        End If
    Next
    Set oServ = Nothing
    Set cProc = Nothing
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:21
Joined
Sep 21, 2011
Messages
14,238
OK, someone on another forum was asking for a way to kill Excel and I supplied the same script, but they tried to run it as VBA? :(
Here is a VBA version.
I had to add the Error Resume, as although it goes through the loop several times, it errors on the second try saying 'does not exist'?
Code:
Sub KillProcess(strProcess As String)
' ProcessKillLocal.vbs
' Sample VBScript to kill a program
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.7 - December 2010
' Modified for VBA Paul Steel 02/07/2022
' ------------------------ -------------------------------'
Dim objWMIService As Object, objProcess As Object, colProcess As Object
Dim strComputer As String, strProcessKill As String
strComputer = "."
strProcessKill = "'" & strProcess & "'"


Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")


Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill)
On Error Resume Next
For Each objProcess In colProcess
    objProcess.Terminate
Next
MsgBox "Just killed process " & strProcessKill

End Sub
 
Last edited:

Isaac

Lifelong Learner
Local time
Yesterday, 18:21
Joined
Mar 14, 2017
Messages
8,777
I would like to second @The_Doc_Man 's remarks.
It shouldn't happen that an IE instance (=> browser tab) you created in code suddenly refers to a different browser tab or window the user created. I suggest you show the code you are using for this.

Besides, I think it is an extremely bad idea to programmatically close other windows or tabs the user opened for himself. This is a foolproof way to make users hate your application. - Leave the user's stuff alone!
There are ways to get the process handle from the name of the window which might be preferable in this case. But it all depends on what your user's expectations have been set to. It could still be appropriate it just depends on what your users have been trained.
 

stu_c

Registered User.
Local time
Today, 02:21
Joined
Sep 20, 2007
Messages
489
Hi all I am so sorry for the late reply, I didn't get any notifications on replies :(
Basically I use the below code to fill out a form on IE, the issue I have is if there is already an IE window open is has a bit of a moment and comes up with the debug window, I was hoping for a way to force to close the first window and then open up another new window that's my reasons for it :(

the only other option would be to have a pop up window saying another window is already open, this must be closed to continue. anyone able to help with this, I am beyond my knowledge now :)

Obviously there is a lot more fields but this is the basics

Code:
Public Function FUNCCompleteForm()
'Input data
    Dim IE As Object
    
'Create InternetExplorer Object

 'IE.Quit
    Set IE = New InternetExplorerMedium
    
With IE
    .Visible = True
    .navigate "http://corpforms"
End With

For Each win In CreateObject("Shell.Application").Windows
    If win.Name Like "*Internet Explorer" Then
         Set IE = win: Exit For
    End If
Next

'Wait for IE to load
With IE
    Do Until .ReadyState = 4
        DoEvents
    Loop

'Input Data Into IE
Set oDoc = IE.Document
    'NewAddress
    oDoc.getElementsByName("NewAddress").Item(0).Value = [Forms]![TBLBookingDetails].[Form]![AddressNew]
 
    oDoc.getElementsByName("Reason").Item(0).Value = "ChangeOfAddress"
    
End With
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:21
Joined
Sep 21, 2011
Messages
14,238
So have you tried my code?
You might need to switch notifications on in your settings.
 

stu_c

Registered User.
Local time
Today, 02:21
Joined
Sep 20, 2007
Messages
489
So have you tried my code?
You might need to switch notifications on in your settings.
Hi Gasman, I did see your code and did have a little play around but I wasn't 100% sure how to integrate that with my original code
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:21
Joined
Sep 21, 2011
Messages
14,238
Hi Gasman, I did see your code and did have a little play around but I wasn't 100% sure how to integrate that with my original code
Well all it does is kill every process with the name you pass to it?

I've tweaked it now to only show a message IF it kills a process.
Call it with killprocess("iexplore.exe")

You can test it out from the immediate window, which is what I have just done

Code:
Sub KillProcess(strProcess As String)
' ProcessKillLocal.vbs
' Sample VBScript to kill a program
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.7 - December 2010
' Modified for VBA Paul Steel 02/07/2022
' ------------------------ -------------------------------'
Dim objWMIService As Object, objProcess As Object, colProcess As Object
Dim strComputer As String, strProcessKill As String
Dim blnKilled As Boolean
strComputer = "."
strProcessKill = "'" & strProcess & "'"


Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")


Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill)
On Error Resume Next
For Each objProcess In colProcess
    blnKilled = True
    objProcess.Terminate
Next
If blnKilled Then
    MsgBox "Just killed all processes for " & strProcessKill
End If

End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom