Lzypenguin
Lzypenguin

Reputation: 955

How can I change this statement to add another If?

I am using the following code to find an active "Internet Explorer" window and interact with it. I am trying to add a message box popup if the user does not have an internet explorer window open, as opposed to just getting a VBA error. Here is my current code:

For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
Next GetIE

And what I want to add is essentially:

If Not GetIE.Name = "Internet Explorer" Then
MsgBox ("You do not have an active Internet Explorer window open")
End IF

But when I added that right below the first code it did not work and gave me an error. I did not write the top section and unsure how to change it to implement the messagebox. Any help is appreciated.

Upvotes: 1

Views: 39

Answers (1)

FaneDuru
FaneDuru

Reputation: 42256

Try this approach, please:

Sub testFindIE()
 Dim GetIE As Object, boolFound As Boolean
 For Each GetIE In CreateObject("Shell.Application").Windows()
    If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then
        boolFound = True
        Exit For
    End If
 Next GetIE
 If Not boolFound Then MsgBox "You do not have an active Internet Explorer window open"
End Sub

Upvotes: 1

Related Questions