views:

93

answers:

2

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
+6  A: 

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

paxdiablo
Where did I write "While ... Then"?
Aaron de Windt
Sorry, must have mis-read the if statement. Still, I'm pretty certain it's wend, not endwhile.
paxdiablo
+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.
David Hall
another hint is to create a HTML file and place your code inside a <script type=vbscript> </script> to test it (using IE, of course)
Rubens Farias
@Rubens, why IE? Or, did you mean "using your browser of choice, of course"?
paxdiablo
@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.
AnonJr
sorry. The question was edited after I saw it the the time.When I saw it he only had written. "|While ... Then| ? I don't think that's quite right.
Aaron de Windt
The reason I used "End Wile" is because I normally work with VB.NET where "End While" is used.
Aaron de Windt
A: 

See also: http://www.w3schools.com/vbscript/vbscript%5Flooping.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.

AnonJr