3

I'm working on a vbscript program and I have got and "Expected Statement" Error. I cannot find the 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
3
  • 3
    @abelenky, don't quote me on this, but I heard tales that there are people on the planet (wise shamans, I think) who can take statements like "I need help with..." and "I have got an 'Expected Statement' Error. I cannot find the error." and somehow work out that there's a question buried in there. I don't know if it's true, just saying that I'm open to the possibility :-) Commented Oct 22, 2009 at 2:05
  • 2
    @paxdiablo. I think you missed abelenky's point. The "questions end with question marks" refrain has nothing to do with abelenky's ability to understand the post. It is an attempt to get the poster to think clearly about the problem. The act of phrasing SO posts as a question aids the poster in pointedly identifying exactly what they need to know. Without asking pointed questions, the post runs the risk of looking like a random mass of brain-spew. Commented Oct 22, 2009 at 19:02
  • 1
    @Kennet, that's an awful lot to read into such a short comment :-) Still, I'll give the benefit of the doubt - I was only trying to be humorous, after all. Very few people (including my wife, unfortunately) understand my oft-dark humor. Commented Oct 23, 2009 at 4:25

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

7 Comments

Where did I write "While ... Then"?
Sorry, must have mis-read the if statement. Still, I'm pretty certain it's wend, not endwhile.
+1 Pretty sure you are right with the wend - Aaron, if you don't know, you can very easily prototype vb-script by creating a .vbs file on your desktop with the code in it. I just did this and with the while syntax in your example I got the Expected Statement error - wend is what you are after.
@Rubens, why IE? Or, did you mean "using your browser of choice, of course"?
@paxdiablo: If you're trying to use a "round about" debugger, you can only use IE. Fx/Chrome/Opera/etc. don't support client-side VBScript.
|
1

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.