首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Instr时类型不匹配

使用Instr时类型不匹配
EN

Stack Overflow用户
提问于 2014-08-20 20:51:46
回答 3查看 2.4K关注 0票数 0

希望你能帮助我解决我的问题,我已经为IE10创建了一个使用vbs的包,它将检查计算机上是否安装了热修复程序,如果缺少一个热修复程序/更新,脚本将中止安装并记录返回代码。

我这里有一个代码,用于检查安装在计算机上的热修复,但我仍然得到使用"Instr“的错误类型不匹配

$

代码语言:javascript
复制
    objShell.run "cmd /c " & chr(34) & "systeminfo >" & strWin & "\Temp\IE10_patches.txt" & chr(34), 0, True

    msgbox strWin & "\Temp\IE10_patches.txt"
    strFile = strWin & "\Temp\IE10_patches.txt"

    WScript.Sleep 5000

    Const ForReading = 1

    Set objFileToRead = objFSO.OpenTextFile(strFile, ForReading)


    Do Until objFileToRead.AtEndOfStream
            strLine = objFileToRead.ReadLine

            If Len(strLine) > 0 Then 
            strText = Trim(strLine)

            'strLen = Len(strLine)
            'msgbox strLen
            'MSGBOX strLine

            'ctr =  ctr+ 1
            'MSGBOX ctr 

出现错误-> If (InStr(strText,"KB2786081")) >0则

代码语言:javascript
复制
        KB2786081_Flag = "True"

        ElseIf (InStr(strText, "KB2731771")) > 0 Then

        KB2731771_Flag = "True"

        ElseIf (InStr(strText, "KB2729094"))  > 0 Then

        KB2729094_Flag = "True"

        ElseIf (InStr(strText, "KB2639308"))  > 0 Then

        KB2639308_Flag = "True"

        ElseIf (InStr(strText, "KB2533623"))  > 0 Then

        KB2533623_Flag = "True"

        ElseIf (InStr(strText, "KB2670838"))  > 0 Then

        KB2670838_Flag = "True"

        End If

    End If

    loop
    objFileToRead.Close
EN

回答 3

Stack Overflow用户

发布于 2015-09-14 21:15:35

我可能在今天遇到这个问题后有了解决方案,然后意识到我做了什么。

在我的代码中的某个地方,我使用了instr作为变量名,这会导致InStr()函数赋值出错,从而产生错误

Microsoft VBScript运行时(0x800A000D)

类型不匹配:'instr‘

因为InStr现在是一个String,而不是一个函数声明。

要解决这个问题,只需将变量赋值更改为Dim in_str即可。

通常不能这样做,因为InStr()是一个函数,如果我不先声明instr并尝试将其赋值给一个字符串,则会得到以下错误

Microsoft VBScript运行时(0x800A01F5)

非法赋值:“instr”

只有在您首先像这样声明instr时才会发生这种情况

代码语言:javascript
复制
'Key is this line without the declaration instr = "test" 
'would fail with an Illegal assignment runtime error.
Dim instr

instr = "test"

'This line will trigger Type Mismatch error if instr has been declared.
If Instr(1, string1, string2) > 0 Then
...
End If
票数 1
EN

Stack Overflow用户

发布于 2020-09-09 09:06:00

为了找到我自己的问题的解决方案,我清理了一下脚本,下面的代码在Windows10上运行。(除了strWinchr(34)以及其他一些小改动,基本上都是一样的。我的计算机将不会有任何这些windows更新,但仍然会抛出类型不匹配错误,如果这是一个问题):

代码语言:javascript
复制
Const ForReading = 1

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.run "cmd /c systeminfo > IE10_patches.txt", 0, True

strFile = "IE10_patches.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToRead = objFSO.OpenTextFile(strFile, ForReading)


Do While Not objFileToRead.AtEndOfStream
        strLine = objFileToRead.ReadLine

        If Len(strLine) > 0 Then 
            strText = Trim(strLine)
            wscript.echo strText
        End If

    If (InStr(strText, "KB2786081")) > 0 Then

        KB2786081_Flag = "True"

    ElseIf (InStr(strText, "KB2731771")) > 0 Then

        KB2731771_Flag = "True"

    ElseIf (InStr(strText, "KB2729094"))  > 0 Then

        KB2729094_Flag = "True"

    ElseIf (InStr(strText, "KB2639308"))  > 0 Then

        KB2639308_Flag = "True"

    ElseIf (InStr(strText, "KB2533623"))  > 0 Then

        KB2533623_Flag = "True"

    ElseIf (InStr(strText, "KB2670838"))  > 0 Then

        KB2670838_Flag = "True"

    End If

Loop
objFileToRead.Close

在处理这个问题的过程中,我记起了我自己的instr类型不匹配错误的原因。使用instr时,如果要使用比较(0 = vbBinaryCompare (默认值)或1= vbTextCompare),则必须使用Start选项。因此,在本例中(以及我的错误),如下所示:

代码语言:javascript
复制
If (InStr(strText, "KB2786081", 1))

需要满足以下条件:

代码语言:javascript
复制
If (InStr(1, strText, "KB2786081", 1))

在这种情况下,当简单地将> 0用作布尔值时,也不需要使用instr。(在原始OP的脚本中也不应该需要它。)

票数 1
EN

Stack Overflow用户

发布于 2014-08-20 21:51:09

尝尝这个

代码语言:javascript
复制
If (InStr(strText, "KB2786081",1)) > 0 Then
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25405239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档