问题
I'm trying to figure out why I would get this error for the code below. I've searched on the web for possible causes but I couldn't find. Please have a look:
Dim descr, code
If WScript.Arguments.Count = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", Chr(34) & Script.ScriptFullName Chr(34) & " Run", , "runas", 1
Else
descr = InputBox("restore point description","Crepo")
If (descr) Then
code = GetObject("winmgmts:\\.root default:Systemrestore").CreateRestorePoint (descr, 0, 100)
If (code) Then
Msgbox "System Restore Point not created (" & code & ") !", 48, "Crepo"
Else
Msgbox "System Restore Point successfully created", 64, "Crepo"
End If
End If
End If
At runtime, if I input anything, i.e. qwerty
I get this error:
Line: 8
Char: 2
Error: Type mismatch: '[string: "qwerty"]'
Code: 800A000D
From my researches, return type of InputBox
is string
and CreateRestorePoint
first argument is also string
. In fact, it works if I call InputBox
directly as the argument.
回答1:
Instead of If (descr) Then
try If (Not IsNull(descr)) Then
.
You may also want to check for empty strings too: If (Not IsNull(descr) and descr <> "") Then
. In fact, as pointed out in the other answer, since the VBScript InputBox function docs note that If the user clicks Cancel, the function returns a zero-length string ("") you definitely need to check for an empty/zero-length string.
回答2:
The If clause needs a boolean (not a string):
>> descr = "qwerty" >> If (descr) Then WScript.Echo "nonsense" >> Error Number: 13 Error Description: Type mismatch
InputBox returns a string or an empty value (see here), but never Null.
来源:https://stackoverflow.com/questions/33350841/vbscript-type-mismatch-error-apparently-not-justifiable