-1

Please help. cant make this code work. It requires an object. Don't know how to resolve this. I see no typo error on my script. I am trying to check if there is an Internet Explorer that is open to https://www.test.com in my PC. Badly needed for a school project. Thanks in advance

Option Explicit
Dim objShell, objShellWindows, i, objIE
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows

For i = 0 to objShellWindows.Count - 1
    Set objIE = objShellWindows.Item(i)
    strURL = objIE.LocationURL
    If InStr(strURL, "https://www.test.com/")Then
        blnFound = True
    End If
Next
Joey
  • 391
  • 3
  • 9
  • 28
  • what are you trying to do? – oracle certified professional Aug 09 '14 at 12:57
  • I want a vbscript that will check if there is an open internet explorer on my PC.. If there is, then it will check if that IE is open to a particular website. – Joey Aug 09 '14 at 13:22
  • Take a look at this answer: http://stackoverflow.com/a/941853/3898606 – oracle certified professional Aug 09 '14 at 13:34
  • Thanks, but it returns the same error I am encountering while using my script above.. What I really wonder is that I am having an error on the line 8 of my script "strURL = objIE.LocationURL" it says object required objIE. – Joey Aug 09 '14 at 13:50
  • it might be enough to check that objIE is not `Nothing`, like the VBScript example on this page: http://msdn.microsoft.com/en-us/library/windows/desktop/bb773970(v=vs.85).aspx#code-snippet-3 – oracle certified professional Aug 09 '14 at 14:35
  • 1
    `strURL` isnt defined. Since you have `Option Explicit` you need to declare all variables. Perhaps you just need `Dim strURL`? – Matt Aug 09 '14 at 17:22
  • @Matt Good point, but that would raise a different error (800A01F4, variable is undefined). – Ansgar Wiechers Aug 09 '14 at 18:23
  • 1
    The only error I get when running the above code is the `Variable is undefined`. Once i defined it the code appears to be running as intended on Window 7 x64 SP1 Professional. Is this all of your code? You should have gotten the same error as me. I am aware that it is not the root cause of your issue however I wonder why you are able to execute that code without the `800A01F4, variable is undefined` – Matt Aug 09 '14 at 19:03

1 Answers1

0

Below code will check all the URLs loaded in the IE,
Note that here bFound is True when sURL is inside .LocationURL, not begin with. Modify to suit your need.

Option Explicit

Const sURL = "https://www.test.com/"
Dim oShell, oWindow, oIE, bFound
Set oShell = CreateObject("Shell.Application")
bFound = False
For Each oWindow In oShell.Windows
    If InStr(1,oWindow.Fullname,"iexplore.exe",vbTextCompare) > 0 Then
        Set oIE = oWindow
        With oIE
            'Wscript.Echo .LocationURL
            bFound = (InStr(1,.LocationURL,sURL,vbTextCompare) > 0)
            If bFound Then Exit For
        End With
        Set oIE = Nothing
    End If
Next
If bFound Then
    Wscript.Echo sURL & " is loaded"
Else
    Wscript.Echo sURL & " is NOT loaded"
End If
Set oShell = Nothing
PatricK
  • 6,375
  • 1
  • 21
  • 25
  • Do **NOT** use global `On Error Resume Next`. **EVER.** If for some reason you must use `OERN`, do it as localized as possible, and put sensible error handlers in place. – Ansgar Wiechers Aug 13 '14 at 08:25
  • @AnsgarWiechers Did I say we should put `OERN` at global level? I assumes the user to use it in a local Sub. If you did down vote this, please reconsider. I have removed OERN now as very beginning of code I needed it to test for an Object being Nothing. I believe DownVote is for Answers that does not relate to the question, not style of coding practise. – PatricK Aug 13 '14 at 22:47
  • Putting a global `OERN` in an answer without due warning or any kind of error handling is setting a bad example. Keep in mind that most people asking for VBScript help here know very little about VBScript and its pitfalls. – Ansgar Wiechers Aug 15 '14 at 11:20