VBScript未定义变量错误

时间:2011-11-03 20:05:09

标签: html scripting vbscript hta

<script language="VBScript">
Option Explicit
' On Error Resume Next


Dim colIPResults, objFile, objFSO, objNIC, objWMI, objWSHNetwork, strAddresses, strIPAddress, strWQL
Const FOR_APPENDING = 8

Sub DestroyObjects()
If IsObject(objFile) Then Set objFile = Nothing
If IsObject(objFSO) Then Set objFSO = Nothing
If IsObject(objWMI) Then Set objWMI = Nothing
If IsObject(objWSHNetwork) Then Set objWSHNetwork = Nothing
' If IsObject() Then Set = Nothing
End Sub


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSHNetwork = CreateObject("WScript.Network")
Set objWMI = GetObject("WinMGMTS:root\cimv2")
Set StrComputer = objWSHNetwork.Computername 
strWQL = "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
Set colIPResults = objWMI.ExecQuery(strWQL)
For Each objNIC In colIPResults
For Each strIPAddress in objNIC.IPAddress
If strAddresses = "" Then
strAddresses = strIPAddress
Else
strAddresses = strAddresses
End If
Next
Next

Document.write("PC Tag Number: " + StrComputer)

If strAddresses ="0.0.0.0" Or strAddresses ="" or strAddresses = "undefined" Then
    Document.write("No Connection Detected")
Else
    Document.write "Network Address - "+ strAddresses
End If

DestroyObjects()
</script>

它一直告诉我变量未定义!

(StrComputer的变量)

1 个答案:

答案 0 :(得分:3)

你从未在第6行宣布过StrComputer。

在这里详细说明,你使用Option Explicit,这意味着你必须在使用它们之前声明所有变量,即使我们在VBScript中。因此,您必须在第6行的Dim语句中包含StrComputer。

Dim colIPResults,objFile,objFSO,objNIC,objWMI,objWSHNetwork,strAddresses,strIPAddress,strWQL,StrComputer。

此外,您似乎更改了命名约定以使用大写首字母而不是小写字母(StrComputer与strComputer)。虽然您可以随心所欲地命名它,但您可能希望保持一致,这样您就不会在阅读时看到仍然看起来正确的错误代码。