2014-09-03 39 views
0

我是VBScripting的新手。對不起,或者缺少必要的信息。我會盡我所能包括我能幫助你的一切。VBScript 80004005;按預期工作了幾個小時,突然不是

我的問題是,當我執行該腳本,我得到以下錯誤:

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

什麼奇怪的是,我一直在運行相同的腳本被多次整天沒有任何問題。現在,當我運行它時,顯示錯誤。腳本中沒有任何更改。我試過重新啓動,但那似乎什麼也沒做。

下面是代碼:

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 

回答

1

最有可能的原因,你的腳本發生故障是在頁面加載時間的變化,或打開外殼瀏覽器和IE窗口等nuber所有的煩惱,因爲你的腳本不在IE加載頁面等待時,它會檢查每個資源管理器窗口,並且即使未找到目標窗口也會繼續。

試試這個代碼:

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 

我已經刪除第二IE的變量,你爲什麼要通過objShellApp.Windows得到IE2?也許我想念什麼..?國際海事組織你已經有IE實例因此得到相同的實例這樣的方式是沒有必要的,只是控制你的實例。此外,我添加了單獨的功能,等待IE來完成頁面加載。

+0

原始代碼不會等待的原因是由於某種原因導航完成後實例丟失。你可以閱讀更多關於它[這裏](http://adminthoughtcollection.blogspot.com/2011/09/accessing-ie-from-vbscript.html)(看起來像它的Windows 7 +問題)IE2實例是因爲我是一個noob :) – 2014-09-05 12:11:45

+0

現在很清楚,一旦我建議類似問題的解決方案,請參閱[部分UPD2在答案](http://stackoverflow.com/a/23232573/2165759)。 – omegastripes 2014-09-05 15:26:10