[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

using Shell.Application to close all IE windows

Posted on 2005-04-28
3
Medium Priority
?
1,636 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:thefallguy
3 Comments
 
LVL 8

Expert Comment

by:hpdvs2
ID: 13890549
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.
0
 
LVL 10

Accepted Solution

by:
edwardiii earned 1000 total points
ID: 13890841
Hi, thefallguy.

I ran into the same problem--after about 5 open IE windows the For loop below would stop closures.  Adding a Do loop did
job.  I tested with 10 IE windows, all URLs were written to Text1, and it all took about 1 second:

     Dim objInternetExplorerShell As Object
     Dim objInternetExplorerShellWindows As Object
     Dim internetExplorerWin As Object
         
     Set objInternetExplorerShell = CreateObject("Shell.Application")
     Set objInternetExplorerShellWindows = objInternetExplorerShell.Windows
 
     Do Until objInternetExplorerShell.Windows = 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 = 0 Then Exit Sub
     
     Loop
 
     
     Set objInternetExplorerShellWindows = Nothing
     Set objInternetExplorerShell = Nothing
0
 
LVL 1

Author Comment

by:thefallguy
ID: 13895303
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question