简体   繁体   中英

VBScript not running depending on the web server

I've a very strange problem with a VBScript: it works fine depending on the server where the web is located.

I have the same web application on two servers with IIS 7.5. In each server, the code is exactly the same.

The VBScript executes some Excel lines and it updates some information at the web application. The problem comes when updating this information. In one of the servers, there's no problem, but in the other, I get the error below:

运行脚本时出错

As the script runs as it would do, I guess there's no syntax error

The code throwing the error is:

With objIE
    If (testing) Then
        .Navigate URLtesting
    Else
        .Navigate URL
        WScript.Sleep 2000
    End If

    WaitWebLoad()
    .document.getElementById(user).Value = userScript
    .document.getElementById(password).Value = passwordScript
    .document.getElementById(logInButton).Click()
    WaitWebLoad()
    .document.getElementByID(loretoLink).Click()
    WaitWebLoad()
    .document.getElementByID(updatePLLink).Click()
    WaitWebLoad()

    Do
        lastRow = activeSheet.Cells(rowIndex, columnPL_ID).Value
        dateAssociated = activeSheet.Cells(rowIndex, columnArrivalDate).Value
        concesion = activeSheet.Cells(rowIndex, columnConcesion).Value
        If lastRow <> "" Then
            .document.getElementByID(searchPL_TB).Value = lastRow
            .document.getElementByID(searchPL_Button).Click()
            WaitWebLoad()
            If (Not (.document.getElementByID(errorLookingPL)) Is Nothing Or Not (.document.getElementByID(noSearchResult) Is Nothing)) Then
                'PL not found
                recordsNotFound = recordsNotFound + 1
                WriteOpenFileText(logFilePath), (Now & vbTab & "***PL not found: " & lastRow & " - [" & dateAssociated & "]" & " - " & concesion & vbCrLf)
            Else ...

The line number 295 is:

If (Not (.document.getElementByID(errorLookingPL)) is Nothing Or Not (.document.getElementByID(noSearchResult) is Nothing)) Then

The WaitWebLoad function code is:

Function WaitWebLoad()
    Dim timer
    timer = 0

    With objIE
        Do While .Busy
            If timer < timerWebLoad Then
                WScript.Sleep 100
                timer = 100
            Else
                'Error loading the web
                objExcel.Workbooks.close
                objExcel.quit
                objIE.quit
                DeleteFile(pathTemp)
                endScriptStamp = Now

                WScript.Echo("Error." & vbCrLf & "Total runtime is: " & DateDiff("s", startScriptStamp, endScriptStamp) & " seconds.")
                WScript.quit
            End If
        Loop
    End With
End Function

I guess the problem is that the script, when running the server where the error takes place, is losing the objIE object, but I don't know why it's becoming lost only in one of the servers.

The getElementById method returns the first object with the same ID attribute as the specified value, or null if the id cannot be found. Reference: .NET Framework and Document Object Model (DOM) .
On the other side, Is operator compares two object reference variables and Null is not an object; it indicates that a variable contains no valid data.
Moreover, if used in VBScript , getElementById method could raise 800A01A8 (=decimal -2146827864 ) Microsoft VBScript runtime error: Object required if used improperly: for instance, as you can't write Set objAny = Null !

Here's a workaround:

On Error Resume Next                                              ' enable error handling
  Set oerrorLookingPL = .document.getElementByID(errorLookingPL)  ' try
  If Err.Number <> 0 Then Set oerrorLookingPL = Nothing           ' an error occurred?
  Err.Clear
  If Vartype(oerrorLookingPL) = 1 Then Set oerrorLookingPL = Nothing   ' Null => Nothing
  Set onoSearchResult = .document.getElementByID(noSearchResult)
  If Err.Number <> 0 Then Set onoSearchResult = Nothing
  Err.Clear
  If Vartype(onoSearchResult) = 1 Then Set onoSearchResult = Nothing
On Error Goto 0                                                   ' disable error handling
If (Not (oerrorLookingPL Is Nothing) Or Not (onoSearchResult Is Nothing)) Then

I can't recommend global use of On Error Resume Next as If condition Then … statement always evaluates given condition to True if a runtime error occurs while evaluating it, see next example:

On Error Resume Next
' show Variant subtype information about Null and Nothing
Wscript.Echo VarType(Null)    & " Null    " & TypeName(Null)
Wscript.Echo VarType(Nothing) & " Nothing " & TypeName(Nothing)
' all conditions as well as their negations are evaluated to `True`
if     (Null  = Nothing) then Wscript.Echo "     Null  = Nothing"
if NOT (Null  = Nothing) then Wscript.Echo "not (Null  = Nothing)"
if     (Null is Nothing) then Wscript.Echo "     Null is Nothing"
if NOT (Null is Nothing) then Wscript.Echo "not (Null is Nothing)"
' show runtime error
On Error GoTo 0
if     (Null is Nothing) then Wscript.Echo "     Null is Nothing"

Both Null = Nothing and Null is Nothing conditions are evaluated to True as well as their negations!

==> cscript D:\VB_scripts\SO\37563820.vbs
1 Null    Null
9 Nothing Nothing
     Null  = Nothing
not (Null  = Nothing)
     Null is Nothing
NOT (Null is Nothing)
==> D:\VB_scripts\SO\37563820.vbs(12, 1) Microsoft VBScript runtime error: Object required

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM