VBScript undefined variable error

别等时光非礼了梦想. 提交于 2019-12-11 18:17:38

问题


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

(variable for StrComputer)


回答1:


You never declared StrComputer on line 6.

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.

Dim colIPResults, objFile, objFSO, objNIC, objWMI, objWSHNetwork, strAddresses, strIPAddress, strWQL, 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.



来源:https://stackoverflow.com/questions/8001136/vbscript-undefined-variable-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!