vbscript 类型不匹配错误显然不合理 [英] Vbscript type mismatch error apparently not justifiable

查看:32
本文介绍了vbscript 类型不匹配错误显然不合理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚为什么我会在下面的代码中收到这个错误.我在网上搜索了可能的原因,但我找不到.请看:

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

在运行时,如果我输入任何内容,即 qwerty,我会收到此错误:
线路:8
字符:2
错误:类型不匹配:'[字符串:qwerty"]'
代码:800A000D根据我的研究,InputBox 的返回类型是 stringCreateRestorePoint 第一个参数也是 string.事实上,如果我直接调用 InputBox 作为参数,它是有效的.

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.

推荐答案

代替 If (descr) Then 尝试 If (Not IsNull(descr)) Then.您可能也想检查空字符串:If (Not IsNull(descr) and descr <> "") Then.事实上,正如在另一个答案中指出的那样,由于 VBScript InputBox 函数文档 注意如果用户点击取消,该函数返回一个零长度字符串("")你肯定需要检查空/零长度字符串.

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.

这篇关于vbscript 类型不匹配错误显然不合理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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