简体   繁体   中英

Type Mismatch Using Instr

Hi hope you could help me out with my problem, I have created a package using vbs for IE10 that would check if there are installed hotfix on the computer, if one hotfix/update is missing the script will abort the installation and log the returncode.

I have here a code for checking the hotfix installed on the computer but Im still getting the error type mismatch with using the "Instr"

$---------------------------------- Checking of installed patches ----------------------------------

    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 

Getting an error here - > 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

I may have the solution after having this issue myself today then realising what I had done.

Somewhere in my code I had used instr as a variable name which then screws up the InStr() function assignment, producing error

Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'instr'

because InStr is now a String not a function declaration.

To fix the problem just changed my variable assignment to Dim in_str .

Usually you can't do this because InStr() is a function if I didn't declare instr first and tried to assign it to a String would get this error

Microsoft VBScript runtime (0x800A01F5)
Illegal assignment: 'instr'

This will only happen if you first declare instr like this

'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

In order to find a solution for my own problem, I cleaned up the script a bit and the following works for me on Windows 10. (It's basically the same without the strWin and chr(34) and a few other minor changes. My computer won't have any of those windows updates, but would still throw the Type Mismatch error if it was an issue):

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

In the midst of messing with this, I remembered the reason for my own instr Type Mismatch error. When using instr , if you're going to use the Compare (0 = vbBinaryCompare (default) or 1 = vbTextCompare), then you must use the Start option. So in this example (and my mistake), the following:

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

Needs to be:

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

The > 0 is not necessary in this case too when simply using instr as a boolean. (It shouldn't be needed in the original OP's script either.)

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM