Link to home
Start Free TrialLog in
Avatar of thefallguy
thefallguy

asked on

using Shell.Application to close all IE windows

I wrote a vbscript that closes Internet explorer windows. The main reason i'm doing it this way it to get the LocationURL property of the objIE object before I call Quit on it.

The problem is that not all windows close this way if you have around 5 open Internet Explorer Windows.

Any suggestions?

Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
dim i, nCount
nCount = objShellWindows.Count
nCount = nCount - 1

for i = 0 to nCount
 dim objIE

 set objIE = objShellWindows.Item(i)
 if (not objIE is nothing) then
   'Do objIE.LocationURL related stuff here
   objIE.Quit
 end if
next
Avatar of Dan Violet Sagmiller (He/Him)
Dan Violet Sagmiller (He/Him)
Flag of United States of America image

I would suggest trying your For/Next statement in reverse.  

For i = nCount to 0 step -1


I would presume that if a windows shuts itself down completely, the ShellWindows Object will refresh.

If this is the case, then once you have Item 0 quit, The next item will take it's place.  So if the windows shut down fast enough, you will only see half of them go away.
ASKER CERTIFIED SOLUTION
Avatar of edwardiii
edwardiii

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thefallguy
thefallguy

ASKER

for anyone looking for a solution... working code.

     Dim objInternetExplorerShell
     Dim objInternetExplorerShellWindows
     Dim internetExplorerWin
         
     Set objInternetExplorerShell = CreateObject("Shell.Application")
     Set objInternetExplorerShellWindows = objInternetExplorerShell.Windows
 
     Do Until objInternetExplorerShell.Windows.Count = 0
     
          For Each internetExplorerWin In objInternetExplorerShellWindows
               On Error Resume Next
         
              'Grab LocationURL info here and write it wherever you'd like.
              'Text1.Text = Text1.Text & " " & internetExplorerWin.LocationURL & vbNewLine
         
              If Err Then
                  Err.Clear
              End If
         
              internetExplorerWin.Quit
              Set internetExplorerWin = Nothing
          Next

          If objInternetExplorerShell.Windows.Count = 0 Then Exit do
Loop
 
     
     Set objInternetExplorerShellWindows = Nothing
     Set objInternetExplorerShell = Nothing