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 + " "
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 = " "
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With