0

I am new to VBScripting. Sorry for any mistakes, or lack of necessary information. I will do my best to include everything I can to help you help me.

My problem is when I execute the script, I get the following error:

  • Line: 22
  • Char: 5
  • Error: Unspecified error
  • Code: 80004005
  • Source: (null)

What is strange is that I had been running the same script multiple times all day without any issue. Now when I run it, the error is displayed. Nothing in the script changed. I have tried rebooting, but that seems to have done nothing.

Here is the code:

Call Main

Function Main
Dim IE
Dim pin
Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
Set objShellApp = CreateObject("Shell.Application")
Set IE2 = WScript.CreateObject("InternetExplorer.Application", "IE_")

pin=inputbox("Pin: ","Enter the pin to continue","")

IE.Visible = True
IE.Navigate "https://ps.hasdk12.org/admin/pw.html"

For Each objWindow in objShellApp.Windows
If LCase(objWindow.LocationName) = LCase("PowerSchool") Then
  Set IE2 = objWindow
End If
WScript.Sleep (5)
Next

With IE2.Document
    .getElementByID("fieldPassword").value = "username;" + pin
    .getElementByID("btnEnter").click
End With

For Each objWindow in objShellApp.Windows
If LCase(objWindow.LocationName) = LCase("Start Page") Then
  Set IE2 = objWindow
End If
WScript.Sleep (5)
Next

End Function
Jeff Click
  • 889
  • 9
  • 20
  • I have to reboot my pc when I get the error and then run again and runs fine..for a while then comes back... I run about 30 different tasks from task manager and they used to blow up more often until i setup a reboot pc task weekly. – phill Aug 19 '21 at 19:17

1 Answers1

1

Most probably reasons why your script become faulty are variations in page loading time, or nuber of opened Shell Explorer and IE windows etc. All troubles because your script doesn't wait while IE loading page, it checks each Explorer window and just continues even if target window isn't found.

Try this code:

Call Main

Function Main()
    Dim oIE
    Dim sPin
    Set oIE = WScript.CreateObject("InternetExplorer.Application", "IE_")
    sPin = InputBox("pin: ","Enter the pin to continue", "")
    oIE.Visible = True
    oIE.Navigate "https://ps.hasdk12.org/admin/pw.html"
    WaitIE oIE, "PowerSchool"
    With oIE.Document
        .getElementByID("fieldPassword").value = "username;" + sPin
        .getElementByID("btnEnter").click
    End With
    WaitIE oIE, "PowerSchool"
End Function

Function WaitIE(oIE, sLocation)
    Do Until (LCase(oIE.LocationName) = LCase(sLocation)) And (Not oIE.Busy) And (oIE.ReadyState = 4)
        WScript.Sleep 5
    Loop
End Function

I've removed second IE variable, why did you get IE2 via objShellApp.Windows? Maybe I miss something..? IMO you already have IE instance hence getting the same instance such way is not necessary, just control that instance you have. Also I've added separate function that waits IE to complete page loading.

omegastripes
  • 12,351
  • 4
  • 45
  • 96
  • The reason the original code doesn't wait is due to, for some reason, the instance being lost after a navigate is done. You can read more about it [here](http://adminthoughtcollection.blogspot.com/2011/09/accessing-ie-from-vbscript.html) (Seems like its a windows 7+ issue) The IE2 instance is because I am a noob :) – Jeff Click Sep 05 '14 at 12:11
  • Now it's clear for me, once I suggested solution for similar problem, see [part UPD2 in answer](http://stackoverflow.com/a/23232573/2165759). – omegastripes Sep 05 '14 at 15:26