【问题标题】:Type Mismatch Using Instr使用 Instr 类型不匹配
【发布时间】:2020-09-09 09:05:59
【问题描述】:

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

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

$--------------------------------- 检查已安装的补丁 ----------------------------------

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

    End If

    loop
    objFileToRead.Close

【问题讨论】:

  • 嗯。你的代码对我来说很好。 If 语句周围的额外括号不是必需的,但它们不会造成任何伤害。
  • 嗨,邦德代码仍然无法正常工作,但我找到了另一种解决方案,它工作正常。我将 Instr 更改为 InstrRev,我不知道这将来是否会出现问题。谢谢
  • 这太疯狂了。 InStrRev()InStr() 都接受两个字符串,所以我不确定为什么一个会起作用,而另一个不起作用。您是否尝试将所有参数指定为 InStr() -- InStr(1, strText, "KB1234567", vbTextCompare)
  • @Bond 我可能有答案,因为我今天愚蠢地遇到了这个问题,然后意识到我已经声明了一个变量 Dim instr 并为其分配了一个字符串,这显然会对 @987654329 的任何地方产生影响然后调用@函数。重命名我的变量解决了这个问题,当时我什至没有想到它有Dim instr, outstr作为输入和输出字符串,真是愚蠢的错误。

标签: vbscript internet-explorer-10 packaging wsh


【解决方案1】:

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

在我的代码中某处我使用了 instr 作为变量名,然后搞砸了 InStr() 函数分配,产生错误

Microsoft VBScript 运行时 (0x800A000D)
类型不匹配:'instr'

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

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

通常你不能这样做,因为InStr() 是一个函数,如果我没有先声明instr 并尝试将它分配给一个字符串会得到这个错误

Microsoft VBScript 运行时 (0x800A01F5)
非法赋值:'instr'

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

'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

【讨论】:

    【解决方案2】:

    为了找到我自己的问题的解决方案,我稍微清理了脚本,以下在 Windows 10 上对我有用。(没有strWinchr(34) 和其他一些小问题基本上是一样的更改。我的计算机不会有任何这些 Windows 更新,但如果这是一个问题,仍然会抛出类型不匹配错误):

    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),则必须使用开始选项。所以在这个例子中(也是我的错误),如下:

    If (InStr(strText, "KB2786081", 1))
    

    需要:

    If (InStr(1, strText, "KB2786081", 1))
    

    > 0 在这种情况下也不是必需的,只需将 instr 用作布尔值。 (在原始 OP 的脚本中也不应该需要它。)

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

      • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
      • 谢谢贾斯汀。下次我会小心的。
      • 还是不行,可能是test os有问题。谢谢。我仍然想知道为什么 Instr 给我类型不匹配错误,尽管我将其更改为 InstrRev 并且工作正常。
      猜你喜欢
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多