VBScript未定义变量错误 [英] VBScript undefined variable error

查看:107
本文介绍了VBScript未定义变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>

它不断告诉我变量未定义!

It keeps telling me that the variable is undefined!

(StrComputer的变量)

(variable for StrComputer)

推荐答案

您从未在第6行声明StrComputer.

You never declared StrComputer on line 6.

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

To elaborate a bit here, you used the Option Explicit which means that you must declare all of your variables before you use them even though we're in VBScript. Because of this you must include StrComputer in your Dim statement on line 6.

昏暗的colIPResults,objFile,objFSO,objNIC,objWMI,objWSHNetwork,strAddresses,strIPAddress,strWQL,StrComputer.

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

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

Also, you appear to have changed the naming convention to use an upper case first letter rather than a lower case (StrComputer vs. strComputer). Although you can certainly name it however you want, you may want to stay consistent so you don't end up with buggy code that still looks right when you read through it.

这篇关于VBScript未定义变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆