By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,641 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,641 IT Pros & Developers. It's quick & easy.

For Each - Doesn't loop all IE objects

P: n/a
Ok here's the deal, the for each statment using VB 6 doesn't loop all
the objects. I would like it to close all the IE browsers that are
open, but it doesn't work. And now I'm concerned that its doing this
elsewhere in my code too, and the RETRY trick isn't an option.
Starting with say 16 open IE windows, this doesn't work, it exits the
for next while still having several IE windows open.
>>>> Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.WebBrowser

For Each IE In SWs
IE.Quit
Next

Set SWs = Nothing
Set IE = Nothing
<<<<<<

However as a work around for this case this does work.
>

Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.WebBrowser

RETRY:
For Each IE In SWs
IE.Quit
Next
If SWs.Count <> 0 Then GoTo RETRY

Set SWs = Nothing
Set IE = Nothing
<<<<<<

But as you can see this isn't usable in many cases when you need to
cycle all the objects.

Is this a bug that has a fix or am I just asking for too much?

(Please pardon the multi-post, the previous was in the wrong topic)
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Did you know that this is a VBNet newsgroups,

There are a lot of newsgroups about VB classic.

Microsoft.public.vb* (there are a bunch)

I hope you find your answer quick.

Cor
Nov 20 '05 #2

P: n/a
Out for blood 74,
In addition to Cor's comments that this is a VB6 newsgroup.
Is this a bug that has a fix or am I just asking for too much? Its rarely a good idea to modify a collection while in a For Each. I would
expect IE.Quit implicitly removes an item from the collection, hence you are
modifing it.

Its not that you are asking too much as you don't realize what is happening
to your collection... In VB.NET a number of collections actually throw an
exception (raise an error) when you attempt to do what you are doing.
For Each IE In SWs
IE.Quit
Next
I strongly suspect you are closing every other window. As the first time
through the loop IE.Quit will close the first window, causing the 2nd window
to become the first one. Then the loop will close the new 2nd window
(formally the 3rd), causing the 4th window to become the 2nd one. Then the
loop will close the 3rd window, formally the 5th, and so on...

When I need to "close" all the items in a collection I normally use a while
loop.

Something like:

While SWs.Count > 0
SWs(0).Quite
End While

There are variations of this loop that uses a For loop with a descending
index (close the last one, then the next to last one, then the 3rd to
last...) using a descending loop is good as you are not shifting elements
one position at a time as the while does.

Hope this helps
Jay
<ou***********@hotmail.com> wrote in message
news:58**************************@posting.google.c om... Ok here's the deal, the for each statment using VB 6 doesn't loop all
the objects. I would like it to close all the IE browsers that are
open, but it doesn't work. And now I'm concerned that its doing this
elsewhere in my code too, and the RETRY trick isn't an option.
Starting with say 16 open IE windows, this doesn't work, it exits the
for next while still having several IE windows open.
>>>>> Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.WebBrowser

For Each IE In SWs
IE.Quit
Next

Set SWs = Nothing
Set IE = Nothing
<<<<<<

However as a work around for this case this does work.
>>>

Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.WebBrowser

RETRY:
For Each IE In SWs
IE.Quit
Next
If SWs.Count <> 0 Then GoTo RETRY

Set SWs = Nothing
Set IE = Nothing
<<<<<<

But as you can see this isn't usable in many cases when you need to
cycle all the objects.

Is this a bug that has a fix or am I just asking for too much?

(Please pardon the multi-post, the previous was in the wrong topic)

Nov 20 '05 #3

P: n/a
Jay,

Please do not disrupt the Garden

:-)

Cor
Nov 20 '05 #4

P: n/a
Doh!
In addition to Cor's comments that this is a VB6 newsgroup.
That should be this is not a VB6 newsgroup, this is a VB.NET newsgroup.

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Out for blood 74,
In addition to Cor's comments that this is a VB6 newsgroup.
Is this a bug that has a fix or am I just asking for too much? Its rarely a good idea to modify a collection while in a For Each. I would
expect IE.Quit implicitly removes an item from the collection, hence you

are modifing it.

Its not that you are asking too much as you don't realize what is happening to your collection... In VB.NET a number of collections actually throw an
exception (raise an error) when you attempt to do what you are doing.
For Each IE In SWs
IE.Quit
Next
I strongly suspect you are closing every other window. As the first time
through the loop IE.Quit will close the first window, causing the 2nd

window to become the first one. Then the loop will close the new 2nd window
(formally the 3rd), causing the 4th window to become the 2nd one. Then the
loop will close the 3rd window, formally the 5th, and so on...

When I need to "close" all the items in a collection I normally use a while loop.

Something like:

While SWs.Count > 0
SWs(0).Quite
End While

There are variations of this loop that uses a For loop with a descending
index (close the last one, then the next to last one, then the 3rd to
last...) using a descending loop is good as you are not shifting elements
one position at a time as the while does.

Hope this helps
Jay
<ou***********@hotmail.com> wrote in message
news:58**************************@posting.google.c om...
Ok here's the deal, the for each statment using VB 6 doesn't loop all
the objects. I would like it to close all the IE browsers that are
open, but it doesn't work. And now I'm concerned that its doing this
elsewhere in my code too, and the RETRY trick isn't an option.
Starting with say 16 open IE windows, this doesn't work, it exits the
for next while still having several IE windows open.
>>>>>>>

Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.WebBrowser

For Each IE In SWs
IE.Quit
Next

Set SWs = Nothing
Set IE = Nothing
<<<<<<

However as a work around for this case this does work.
>>>>>>>

Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.WebBrowser

RETRY:
For Each IE In SWs
IE.Quit
Next
If SWs.Count <> 0 Then GoTo RETRY

Set SWs = Nothing
Set IE = Nothing
<<<<<<

But as you can see this isn't usable in many cases when you need to
cycle all the objects.

Is this a bug that has a fix or am I just asking for too much?

(Please pardon the multi-post, the previous was in the wrong topic)


Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.