VBscript"预期语句"错误

Aar*_*ndt 3 vbscript

我正在研究一个vbscript程序,我得到了"预期声明"错误.我找不到错误.我已经看到了这个错误的一些样本,但他们没有帮助我.

我是vbscript的新手.

这是代码.

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
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 7

While ... End While?我不认为这是对的.

我认为语法是:

While counter > 0
    ...
Wend
Run Code Online (Sandbox Code Playgroud)

试试这个,它有一些其他的改进(我很确定Mid使用基数1,而不是基数0 - 如果没有改变For t = 1 to Len (tx)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
Run Code Online (Sandbox Code Playgroud)

我没有测试过这种彻底(当然,在所有的,真的),所以让我知道,如果有一个问题(或只是恢复到原来的解决方案,取代End WhileWend,并可能改变的范围t为基数-1 Mid).