开发者

VBscript "Expected Statement" Error

开发者 https://www.devze.com 2022-12-09 06:38 出处:网络
I\'m working on a vbscript program and I have got and \"Expected Statement\" Error. I cannot find t开发者_如何学运维he error. I\'ve seen some samples of this error but they didn\'t help me.

I'm working on a vbscript program and I have got and "Expected Statement" Error. I cannot find t开发者_如何学运维he error. I've seen some samples of this error but they didn't help me.

I am new to vbscript.

Here is the code.

Sub SetText(tx, lw)
    Dim t, l, r, a

    t = -1
    l = Len(tx)
    r = ""
    a = 0

    While t < l
        t = t + 1
        a = Asc(Mid(tx,t,1))

        If a >= 160 or a=60 or a=62 or a=38 or a=34 or a=39 or a=32 Then
            If a = 32 Then
                r = r + "&nbsp;"
            Else
                r = r + "&#" + Cstr(a) + ";"
            End If
        Else
            r = r + Mid(tx,t,1)
        End If

    End While 'The error occurs at the beginning of this statement.'

    If Not lw Then
        r = "<pre>" + r + "</pre>"
    End If

    r = "<div style='width:auto; height:auto;'>" + r + "</div>"        
    objExplorer.document.body.innerHTML = r
End Sub


While ... End While ? I don't think that's quite right.

I think the syntax is:

While counter > 0
    ...
Wend

Try this instead, it has some other improvements (I'm pretty certain Mid uses base 1, not base 0 - if not change the For t = 1 to Len (tx) to For t = 0 to Len (tx) - 1):

Sub SetText (tx, lw)
    Dim t, r, c, a

    'Standard prefix and optional pre tag.'
    r = "<div style='width:auto; height:auto;'>"
    If Not lw Then
        r = r + "<pre>"
    End If

    'Process each character in string.'
    For t = 1 to Len (tx)
        'Get character and code.'
        c = Mid (tx,t,1)
        a = Asc (c)

        'Change "character" if it is one of the special ones.'
        If a = 32 Then
            c = "&nbsp;"
        Else
            If a >= 160 or a = 60 or a = 62 or a = 38 or a = 34 or a = 39 Then
                c = "&#" + Cstr (a) + ";"
            End If
        End If

        'Add "character" to result (it may be a string at this point).'
        r = r + c
    Next

    'Optional pre tag and standard suffix.'
    If Not lw Then
        r = r + "</pre>"
    End If
    r = r + "</div>"

    'Inject into page.'
    objExplorer.document.body.innerHTML = r
End Sub

I haven't tested this thoroughly (well, at all, really) so let me know if there's a problem (or just revert to your original solution, replacing End While with Wend, and possibly changing the range of t for base-1 Mid).


If everything is syntactically correct, if you copy and paste the code from a location not compatible with QTP, QTP may not recognize the code and throw 'Expected identifier/statement'.

Simply retyping the code exactly as it was will resolve this.


See also: http://www.w3schools.com/vbscript/vbscript_looping.asp

paxdiablo's answer is right - you don't use End While in VBScript. Your code should look like this:

Sub SetText(tx, lw)
    Dim t, l, r, a

    t = -1
    l = Len(tx)
    r = ""
    a = 0

    While t < l
        t = t + 1
        a = Asc(Mid(tx,t,1))

        If a >= 160 or a=60 or a=62 or a=38 or a=34 or a=39 or a=32 Then
            If a = 32 Then
                r = r + "&nbsp;"
            Else
                r = r + "&#" + Cstr(a) + ";"
            End If
        Else
            r = r + Mid(tx,t,1)
        End If

    Wend '<---'

    If Not lw Then
        r = "<pre>" + r + "</pre>"
    End If

    r = "<div style='width:auto; height:auto;'>" + r + "</div>"        
    objExplorer.document.body.innerHTML = r
End Sub

Last I checked, its a legacy control structure and is generally discouraged. Your code should probably really look like this:

Sub SetText(tx, lw)
    Dim t, l, r, a

    t = -1
    l = Len(tx)
    r = ""
    a = 0

    Do While t < l
        t = t + 1
        a = Asc(Mid(tx,t,1))

        If a >= 160 or a=60 or a=62 or a=38 or a=34 or a=39 or a=32 Then
            If a = 32 Then
                r = r + "&nbsp;"
            Else
                r = r + "&#" + Cstr(a) + ";"
            End If
        Else
            r = r + Mid(tx,t,1)
        End If

    Loop

    If Not lw Then
        r = "<pre>" + r + "</pre>"
    End If

    r = "<div style='width:auto; height:auto;'>" + r + "</div>"        
    objExplorer.document.body.innerHTML = r
End Sub 

Actually, there are probably more changes, but not being familiar with the context of this code that's as far as I'll advise.


I had the same issue.

I got an error saying "Expected Statement" error at line 1 itself. I had copied the code from a previous Business Component in UFT to Notepad++ file in .txt format temporarily. From here I had copied and pasted to new Business Component file.

@William Humphries' solution worked for me. But no need to type all the way again. Solution is simple.
I saved my code into a .vbs file in notepad++. Discarded the Business component file where I was getting error. Created a fresh new business component/Action file. Copied and pasted from .vbs notepad++ file into new business component. It worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消